8 Replies Latest reply: Dec 31, 2013 2:21 PM by user10284167 RSS

Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?

216944 Newbie
Currently Being Moderated
I am trying to fetch messages 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") "<53498286-6B3E-4AC8-8CA0-481152C80968@xxxx.it>" 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 !
  • 1. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    gimbal2 Guru
    Currently Being Moderated
    Check the version of JavaMail you're using. If I have to believe the results of a Google query I did, JavaMail 1.4.1 and up should not have this problem anymore. Try to use the latest version (which seems to be 1.4.5).
  • 2. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    216944 Newbie
    Currently Being Moderated
    I am using 1.4.5, because I discovered the problem in 1.4.1 and tried to upgrade to see if it had been fixed :-(
  • 3. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    bshannon Pro
    Currently Being Moderated
    I happened to see your stackoverflow post first, so I answered there first.

    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.
  • 4. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    7460bfb0-6955-47fe-81a4-9c505e820ef8 Newbie
    Currently Being Moderated

    I am currently experiencing the same null pointer in javax mail 1.4.7 - 1.5.0 when calling getContent() on a message. Confirmed via maven dependency tree that only one instance of the javax mail is present.

     

    Is there any update on what the solution to this problem is or workarounds?

  • 5. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    bshannon Pro
    Currently Being Moderated

    This has nothing to do with the maven dependency tree.  Look at your CLASSPATH setting, the various "lib" directories in your JRE, app server, etc., and all the stuff packaged with your application.  If you need help knowing where to look, describe more about the environment in which your application is running.

  • 6. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    7460bfb0-6955-47fe-81a4-9c505e820ef8 Newbie
    Currently Being Moderated

    The environment setup is a Google App Engine setup using the latest sdk that facilitates IMAP. The classpath/lib directory only contains a single javax mail version. Utilising the gimap library to obtain the contents ( thread id, message id ). Dependency management handled by maven, which excluding javax mail that is included as part of com.sun.mail:gimap:1.4.7 results in class not found confirming that this is the only dependency bringing in the javax mail libraries.

     

    Using 1.4.7 due to version 1.5.0 of gimap results in Field not found SIZE when performing a fetch on messages:

     

    The code section experiencing the problem is:

               Security.addProvider( new OAuth2Provider() );

     

                Session session = getImapSession( oAuthToken );

     

                GmailSSLStore store = getImapStore( session, userEmail );

                GmailFolder folder = (GmailFolder ) store.getFolder("INBOX");

                folder.open( Folder.READ_ONLY);

                Message[] ms = folder.search( searchTerm );

                FetchProfile fp = new FetchProfile();

                fp.add(GmailFolder.FetchProfileItem.MSGID);

                fp.add(GmailFolder.FetchProfileItem.THRID);

                fp.add(GmailFolder.FetchProfileItem.LABELS);

                folder.fetch(ms, fp);

                GmailMessage gm;

                String[] labels;

     

                for (Message m : ms) {

                    gm = (GmailMessage) m;

                    InternetAddress sender = ( InternetAddress ) gm.getSender();

                    String content = "";

                    try

                    {

                        Object messageContent = gm.getContent(); //Exception on this line for null pointer exception

     

     

                        if( messageContent instanceof String )

                        {

                            content = ( String ) messageContent;

                        }

                        else if( messageContent instanceof Multipart )

                        {

                            Multipart mp = ( Multipart ) messageContent;

                            content = mp.toString();

     

     

                        }

         ....

         ....

     

    This source is obtained from https://code.google.com/p/java-gmail-imap/wiki/TestingJavaMailGIMAP with the only difference of using OAuth 2.0 intead of straight username and password. Running the section of code via a test servlet.

  • 7. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    bshannon Pro
    Currently Being Moderated

    I suspect that Google App Engine is providing their own version of the javax.mail classes that conflict with the versions you're including in your application.  I think there's a way to tell the Servlet container to use your classes in preference to the classes provided by the runtime.

  • 8. Re: Null pointer exeception in BODYSTRUCTURE.parseParameters. Is it a bug ?
    user10284167 Newbie
    Currently Being Moderated

    The root cause is in fact that the artifact appengine-api-1.0-sdk provides it's own version of javax.mail.internet.MimeMessage which I find appalling from Google.

     

    For those struggling with this problem, you'll have to ensure that your classpath first loads the javax.mail artifact before loading appengine-api-1.0-sdk. I encountered this issue while using camel component (camel-mail).

     

    I'll notify the Camel guys since this is concerning.

     

    Best Regards,

    Richard Burton

    http://www.IamIngenium.com

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points