I'm using java mail API 1.4.7. Protocol is IMAP. I'm trying to filter messages by using a time period.
Date fromTime = new Date();
Date toTime = new Date();
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
//create the search term
SearchTerm term = createSearchTerm(fromTime, toTime);
// retrieve the messages from the folder in an array
Message[] messages = emailFolder.search(term);
Last line of the code trows this exception.
java.lang.NullPointerException
at javax.mail.search.AndTerm.match(AndTerm.java:106)
at javax.mail.search.AndTerm.match(AndTerm.java:106)
at javax.mail.Message.match(Message.java:705)
at javax.mail.Folder.search(Folder.java:1270)
at com.sun.mail.imap.IMAPFolder.search(IMAPFolder.java:1918)
at javax.mail.Folder.search(Folder.java:1231)
at com.sun.mail.imap.IMAPFolder.search(IMAPFolder.java:1873)
etc.
createSearchTerm function
SearchTerm term = null;
ReceivedDateTerm minDateTerm = new ReceivedDateTerm(ComparisonTerm.GE, startTime);
ReceivedDateTerm maxDateTerm = new ReceivedDateTerm(ComparisonTerm.LE, endTime);
term = new AndTerm(term, minDateTerm); //concat the search terms
term = new AndTerm(term, maxDateTerm);
return term;
Without the search function I can read emails just fine. Can some one help me to find out what wrong with my search function?