hugob0ss
hugob0ss

Reputation: 31

NullPointerException when trying to print a mail message

i have a program that uses javamail to retrieve messages from an exchange server using imap protocol then i process each messages that has been unseen.

Recently i have come across with the problem of the program throwing a nullpointerexception i know that this is do to a unitialized object that is invoking a method, but in this case it does not make any sense to me.

The stacktrace of the exception is as follows:

java.lang.NullPointerException
at com.sun.mail.imap.IMAPFolder.handleResponse(IMAPFolder.java:1905)
at com.sun.mail.iap.Protocol.notifyResponseHandlers(Protocol.java:135
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:617)
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1027)
at com.sun.mail.imap.IMAPMessage.getContentType(IMAPMessage.java:321)
at javax.mail.internet.MimeBodyPart.isMimeType(MimeBodyPart.java:802)
at javax.mail.internet.MimeMessage.isMimeType(MimeMessage.java:851)
at ctt.mail.FileHandler.putFilesFromMail(FileHandler.java:75)
at ctt.mail.FileHandler.main(FileHandler.java:37)

aditionaly i've got the debug trace from the last processed message A607 FETCH 202 (FLAGS)

* 202 FETCH (FLAGS (\Seen))

A607 OK FETCH completed.

A608 FETCH 202 (ENVELOPE INTERNALDATE RFC822.SIZE)

* 202 FETCH (ENVELOPE ("Mon, 14 Jul 2014 15:33:56 +0100" "=?iso-8859-1?Q?GPESE_-_    Pr=E9=5FAviso_de_Expedi=E7=E3o?=" (("[email protected]" NIL "SRS0+83ffd8147b51e356=4J=solicitador.net=gpese" "solicitador.net")) NIL NIL (("=?iso-8859-1?Q?RECE=C7=C3O_FICHEIROS_CTT_WINREG?=" NIL "cttwinreg" "ctt.pt")) (("[email protected]" NIL "caraujo" "caso.pt")) NIL NIL "<[email protected]>") INTERNALDATE "14-Jul-2014 15:33:58 +0100" RFC822.SIZE 6462)

A608 OK FETCH completed.

A609 FETCH 202 (BODYSTRUCTURE)

* 202 FETCH (BODYSTRUCTURE (("text" "plain" ("charset" "iso-8859-1") NIL NIL "quoted-printable" 0 0 NIL NIL NIL NIL)("text" "xml" ("name" "GPESE - GE_369515.xml") NIL "GPESE - GE_369515.xml" "base64" 5202 67 NIL ("attachment" ("filename" "GPESE - GE_369515.xml" "size" "3865" "creation-date" "Mon, 14 Jul 2014 14:33:58 GMT" "modification-date" "Mon, 14 Jul 2014 14:33:58 GMT")) NIL NIL) "mixed" ("boundary" "_002_18909390617011405348433043JavaMailgpesesolicitadornet_") NIL "pt-PT"))

* 24457 FETCH (FLAGS (\Seen))

* 24458 FETCH (FLAGS (\Seen))

* 24459 FETCH (FLAGS (\Seen))

* 24460 FETCH (FLAGS (\Seen \Recent))

* 24461 FETCH (FLAGS (\Seen \Recent))

* 24462 FETCH (FLAGS (\Seen \Recent))

A609 OK FETCH completed.

the line that is throwing the exception is in this case a debug line :

System.out.println("Mensagem n.º " + i + " de " + (new InternetAddress()).toString(message[i].getFrom()) + " com o assunto " + message[i].getSubject() + " recebida em " + (new SimpleDateFormat("dd/MM/yyyy HH:mm:ss")).format(message[i].getReceivedDate()) + ". Nova? " + msgFlags.contains(javax.mail.Flags.Flag.RECENT) + ". Tem attachments mixed? " + message[i].isMimeType("multipart/mixed") + ". Tem attachments alternative? " + message[i].isMimeType("multipart/alternative") + ". Tem attachments alternative? " + message[i].isMimeType("") + ". O mime type \351? " + message[i].getContentType());

So the problem in this case is in one of the message[i].isMimeType(); but from my experience the exception can be thrown from another method such as message[i].getFlags().

I've tested message[i] for null and it's not null, thats why i dont understand why the exception is been thrown.

Any help would be appreciated. Thanks.

edit: Some additional stack trace exception that i have collected

java.lang.NullPointerException
at com.sun.mail.imap.IMAPFolder.handleResponse(IMAPFolder.java:1905)
at com.sun.mail.iap.Protocol.notifyResponseHandlers(Protocol.java:135)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchFlags(IMAPProtocol.java:745)
at com.sun.mail.imap.IMAPMessage.loadFlags(IMAPMessage.java:1093)
at com.sun.mail.imap.IMAPMessage.getFlags(IMAPMessage.java:704)
at ctt.mail.FileHandler.putFilesFromMail(FileHandler.java:58)


java.lang.NullPointerException
at com.sun.mail.imap.IMAPFolder.handleResponse(IMAPFolder.java:1905)
at com.sun.mail.imap.IMAPFolder.handleResponses(IMAPFolder.java:1926)
at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:985)
at com.sun.mail.imap.IMAPMessage.getReceivedDate(IMAPMessage.java:278)
at ctt.mail.FileHandler.putFilesFromMail(FileHandler.java:63)

Upvotes: 1

Views: 1025

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

What version of JavaMail are you using? It looks like the NullPointerException is occurring in the JavaMail IMAP provider. From the stack trace it looks like this might be a very old version of JavaMail.

Upvotes: 1

Related Questions