0

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?

Asanka sanjaya
  • 1,461
  • 3
  • 17
  • 35
  • 1
    Please post the body of `createSearchTerm`. – Andy Turner Jan 20 '16 at 13:15
  • maybe `term` is null or not valid? – Jens Jan 20 '16 at 13:16
  • @Jens I don't think so - it would have spewed at `Message.java:705` in that case. It's that one of the terms in the "and" expression is null. – Andy Turner Jan 20 '16 at 13:17
  • Added the createSearchTerm function – Asanka sanjaya Jan 20 '16 at 13:19
  • `term = new AndTerm(term, minDateTerm); `. `term` in your `andTerm()` method is null - this may lead to problems. – LordAnomander Jan 20 '16 at 13:20
  • 1
    I was referring to http://stackoverflow.com/questions/19536386/retrieve-email-according-to-specified-date-time-using-pop3-in-java – Asanka sanjaya Jan 20 '16 at 13:22
  • 1
    You are creating an `AndTerm` between `null` and `minDateTerm` in Line 6. So the first term to be checked is `null`, thus you get a `NullPointerException` when it tries to evaluate it. Remove `SearchTerm term` and change those 3 lines to just `return new AndTerm(minDateTerm, maxDateTerm);` – jbx Jan 20 '16 at 13:23
  • @jbx I think you are correct. Please post that as an answer. So I can mark it as correct after testing. – Asanka sanjaya Jan 20 '16 at 13:25
  • @Asankasanjaya Question closed for answers due to being duplicate. Glad it worked. As a general suggestion, try to avoid initialising references to `null` as much as possible unless absolutely needed. Often rearranging the code a little like in this case can help you avoid it completely (and it will be a bit more efficient too). – jbx Jan 20 '16 at 13:27
  • @jbx Thank you for for the help and advice. No idea why a duplicate though! – Asanka sanjaya Jan 20 '16 at 13:29
  • @Asankasanjaya everything that is about NPE is marked as duplicate, because usually the problems are equal - just the way of resolving the error might vary :) – LordAnomander Jan 20 '16 at 13:30
  • @Asankasanjaya It is because it is the classic case of a `NullPointerException`, which happens when an object which is assumed to be always initialised to something, is sometimes not. It doesn't matter whether you are using anything else, the problem is often the same thing, a mistake that the programmer fails to initialise something or leaves something set to `null` by mistake. In your case the first term used in the first `AndTerm` was `null`, so you got an NPE. Just learn how to find these problems for next time. – jbx Jan 20 '16 at 13:32
  • Got it. Thanks all for the help and advises :) – Asanka sanjaya Jan 20 '16 at 13:38

0 Answers0