Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

NullPointerException while fetching messages

843830Nov 3 2006 — edited Aug 30 2007
Hi
I am getting NullPointerException while trying to fetch messages from a mailbox using IMAP. Following is the code.
            
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore(IMAP_PROTOCOL); 
store.connect(IMAP_HOST, IMAP_PORT, IMAP_USERNAME, IMAP_PASSWORD);

folderMailbox = store.getDefaultFolder();
folderMailbox = folderMailbox.getFolder(IMAP_MAILBOX);
folderMailbox.open(Folder.READ_WRITE);
int totalMessages = folderMailbox.getMessageCount();
            
if (totalMessages == 0) {
    System.out.println("Mailbox is Empty");
}
System.out.println("Mailbox has: " + totalMessages + " messages");
            
Message[] msgs = folderMailbox.getMessages();
// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();  
fp.add(FetchProfile.Item.ENVELOPE);  
fp.add(FetchProfile.Item.CONTENT_INFO);
fp.add(FetchProfile.Item.FLAGS);
folderMailbox.fetch(msgs, fp);

for (int i=1; i<msgs.length; i++){
      System.out.println("msgs : " +msgs.getReceivedDate());
System.out.println("Content type : " +msgs[i].getContentType());
}
folderMailbox.close(true);
store.close();


The exception I am getting is as follows:

java.lang.NullPointerException
at javax.mail.internet.ParameterList.set(ParameterList.java:220)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:278)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:165)
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:146)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:55)
at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:124)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:230)
at com.sun.mail.iap.Protocol.command(Protocol.java:263)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1234)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1215)
at com.sun.mail.imap.IMAPMessage.fetch(IMAPMessage.java:1015)
at com.sun.mail.imap.IMAPFolder.fetch(IMAPFolder.java:902)
at TestProgram.main(TestProgram.java:52)

I always get this Exception while calling the fetch method. Can anyone let me know what I might be doing wrong.
Thanks in advance.

Rohit

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 27 2007
Added on Nov 3 2006
23 comments
1,361 views