I am trying to fetch messages using JavaMail 1.4.5 from an IMAP account and I get a null pointer exception in BODYSTRUCTURE.parseParameters method.
Looking the parseParameters code, I find this line
list.set(null, "DONE"); // XXX - hack
The problem is that the set method tries to call .toLowerCase() to the null value !!!
The response it is trying to parse is this:
* 1 FETCH (BODYSTRUCTURE (("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 55 4 NIL NIL NIL NIL)(("TEXT" "HTML" ("CHARSET" "us-ascii") NIL NIL "7BIT" 410 10 NIL NIL NIL NIL)("IMAGE" "JPEG" ("NAME" "image.jpg") "<[email protected]>" NIL "BASE64" 536628 NIL ("inline" ("FILENAME" "image.jpg")) NIL NIL) "RELATED" ("TYPE" "text/html" "BOUNDARY" "Apple-Mail=_56FA3EC6-FB02-4882-A1C5-487652E3B4E5") NIL NIL NIL) "ALTERNATIVE" ("BOUNDARY" "Apple-Mail=_CB164992-2501-4351-94D1-61CE7C8D90DC") NIL NIL NIL))
and, enabling the debug, I get those messages:
DEBUG IMAP: parsing BODYSTRUCTURE
DEBUG IMAP: msgno 1
DEBUG IMAP: parsing multipart
DEBUG IMAP: parsing BODYSTRUCTURE
DEBUG IMAP: msgno 1
DEBUG IMAP: single part
DEBUG IMAP: type TEXT
DEBUG IMAP: subtype PLAIN
DEBUG IMAP: parameter name CHARSET
DEBUG IMAP: parameter value us-ascii
and then the NullPointerException
Exception in thread "main" java.lang.NullPointerException
at javax.mail.internet.ParameterList.set(ParameterList.java:165)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:404)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:224)
at com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:109)
at com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:158)
at com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:67)
at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:136)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:270)
at com.sun.mail.iap.Protocol.command(Protocol.java:313)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1529)
at com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:1521)
at com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1221)
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1307)
at com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:623)
at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927
Thanks to anyone that can help me !
In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
you can do: Objects. requireNonNull(param); and it will throw a NullPointerException if the parameter you pass it is null .
Using Ternary operator We can also use the ternary operator to avoid the null pointer exceptions. We can put the check using the ternary operator. We check the Boolean expression and return value 1 if the expression is true; otherwise, return value 2.
I finally discovered the cause of the problem.
I'm including Apache Cxf in my project.
Cxf includes a reference to geronimo-javamail_1.4_spec which overwrites some of the javamail classes.
Excluding the reference to geronimo, everything works correctly !
You've probably got a mix of JavaMail classes from two different versions of JavaMail. Check your classpath for other instances of the javax.mail.* classes, perhaps in a j2ee.jar or javaee.jar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With