mercredi 29 novembre 2017

JavaMail message.getSubject().equals gives NullPointerException

I'm having a problem with JavaMail and I just can't understand it. I want to search trough my mailbox for a mail with a certain subject (1234 in this case). I'm getting the subject with message.getSubject() and put it in an if statement and look if it's equal to 1234, but it gives me a NullPointerException every time. The weird thing is that if I'm trying to just printout the subjects of the mails (without the if statement), nothing seems to be wrong.

This is the code:

Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);

//Get all messages from inbox
Message[] messages = inbox.getMessages();

for (int i = 0; i < messages.length; i++) {
    if (messages[i].getSubject().equals("1234")) {
         System.out.println("Message found");
    }
}

This is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

At the following line:

if (messages[i].getSubject().equals("1234")) {

I hope you guys can help me with this problem.

Aucun commentaire:

Enregistrer un commentaire