Menu

NullPointerException mbox corrected Thread

Help
Ulricoher
2005-06-14
2013-05-01
  • Ulricoher

    Ulricoher - 2005-06-14

    Hi everyone,
    Sorry for posting my request once more, but I made some mistakes and tried to format the text;.(

    I have a question. I have tried to figure out how to copy messages from the mail server to a local directory:

        Session session = Session.getDefaultInstance(new Properties());
        store = session.getStore(POP_MAIL);
        store.connect(pop3Host, -1, _user, _password);
        Folder folder = store.getDefaultFolder().getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        Message[] messages = folder.getMessages();

        Store storeLocal = new MStorStore(Session.getDefaultInstance(p),
                       new URLName("mstor:c:/mail"));
        storeLocal.connect();

        Folder localInbox = storeLocal.getDefaultFolder().getFolder("Inbox");
        localInbox.open(Folder.READ_WRITE);
        folder.copyMessages(messages, localInbox);

    I don't know if this is correct, I haven't found any appropriate code sample, so I tried this, but I get a NullPointerException at
    :
        java.lang.NullPointerException
        at net.fortuna.mstor.MStorFolder.appendMessages(Unknown Source)
        at javax.mail.Folder.copyMessages(Folder.java:1071)
        at com.bau.app.mail.MailClientImpl.getNewMessages(MailClientImpl.java:162)
        at com.bau.app.mail.Main.main(Main.java:40)

    The NullPointerException is thrown because in the MStorFolder.appendMessages the MboxFile has not been instantiated.

    Now my question:
    1. Is it ok just to create a local folder c:/mail/Inbox?
    In the open Methode of MStorFolder the Mbox is instantiated as followed:
           
           if ((getType() & HOLDS_MESSAGES) > 0) {
                if (mode == READ_WRITE) {
                    mbox = new MboxFile(file, MboxFile.READ_WRITE);
                }
                else {
                    mbox = new MboxFile(file, MboxFile.READ_ONLY);
                }
            }

    Because in my case getType returns HOLDS_FOLDERS, the condition (getType() & HOLDS_MESSAGES) will be 0,
    so the mbox will not be instantiated and that will lead to NullPointerException.

    I have tried to test the folder with the JUnit TestCase MStorFolderTest and for the method testGetType() the assertion fails.

    I would be very glad if you could help me.

    Thanks a lot in advance,
    Ulrich

     
    • Ben Fortuna

      Ben Fortuna - 2005-06-15

      Hi Ulrich,

      In short, this is a bug in mstor that I have now fixed (thanks for pointing it out). The correct functionality for this code is that mstor should throw a FolderNotFoundException when you try to open the "Inbox" folder.

      Because the folder "Inbox" does not yet exist (you can check this via javax.mail.Folder.exists()), you need to create the folder before you open it, as follows:

          localInbox.create(Folder.HOLDS_MESSAGES);

      Also, whilst your code will work, you probably shouldn't be instantiating an net.fortuna.mstor.MStorStore directly, but rather should rely on the url protocol to determine the type of store, as follows:

          Session session = Session.getDefaultInstance(new Properties());

          Store store = session.getStore(new URLName("mstor:c:/mail"));
          store.connect();

      regards,
      ben

       
    • RustyShelf

      RustyShelf - 2005-06-15

      Hi Ben,

      I'm having a very similar problem, but my folder does actually exist. The stack trace is:
      15/06/2005 10:38:26 net.fortuna.mstor.data.MetaFolderImpl getMessage
      WARNING: Message not MIME message - no metadata available
      java.lang.NullPointerException
          at net.fortuna.mstor.data.MetaFolderImpl.getMessage(Unknown Source)
          at net.fortuna.mstor.MStorFolder.appendMessages(Unknown Source)
          at javax.mail.Folder.copyMessages(Folder.java:1136)
          at net.ivanovic.synchingthunder.SynchingThread.copyMessage(SynchingThread.java:265)
          at net.ivanovic.synchingthunder.SynchingThread.syncFolders(SynchingThread.java:165)
          at net.ivanovic.synchingthunder.SynchingThread.run(SynchingThread.java:68)
          at java.lang.Thread.run(Unknown Source)

      the line that's causing the problem is:
      from.copyMessages(messageToCopy, to);

      where 'from' is the folder that it's coming from, 'messageToCopy' is an array of messages of size 1 and 'to' is the folder that it should be copied to. Now I know the error complains that the message is not a mime message, but it actually is a MIME message, and worked fine with v0.9.4 of mstor. Any ideas why?

      cheers,

      Rusty

       
      • Ben Fortuna

        Ben Fortuna - 2005-06-15

        Hi Rusty,

        This error was a bit deceiving due to the warning message not being entirely correct.

        A MIME message is basically a modern-day email message, which allows for multi-part messages, etc. In the past I used to use a MIME message's message-id header to identify the metadata associated with a message. What I found, however, is that many email messages don't have a message-id header, and as a result I changed the implementation to use the message number as the identifier (again there are problems with this approach, but it works better than the message-id).

        Anyway, the error message you are seeing is more a warning than an error, in that although mstor couldn't create metadata you can still access the actual message. Nevertheless its a warning that shouldn't be happening.

        Now the actual cause of this is a result of a change I made in 0.9.4 where I "prune" messages on the fly in the appendMessages() method. The problem is that I was pruning (i.e. setting to null) a message before retrieving metadata, thus causing a NullPointerException.

        I've now fixed this problem, however if you want to get rid of this warning immediately you can just disable metadata using the following system property:

        mstor.meta.enabled=false

        I'm guessing you don't need the metadata for your project anyway (it will probably run a bit faster if you disable it as well).

        regards,
        ben

         
    • RustyShelf

      RustyShelf - 2005-06-15

      Hi Ben,

      I think you missed my main problem - the NullPointerException in the mstor code. The warning does not really concern me that much. I tried disabling the meta data, and it still happens. I'm using the following code:
      Properties p = new Properties();
               p.setProperty("mstor.mbox.cacheBuffers", "false");
               p.setProperty("mstor.mbox.useNioMapping", "false");
               p.setProperty("mstor.meta.enabled", "false");
               Session session = Session.getDefaultInstance(p);

      The operation actually seems to work - I'm copying the message to the trash folder, and then deleting it, but the NullPointerException kills off the Thread that I'm using in my program, so that it can't do the other things it needs to.

      To clarify, the messages that I'm working with are MIME messages, and the problem did not occur with version 0.9.4, but does with 0.9.5.

      By the way, the deleteOnExit problem seems to be fixed now (although for my work at least, it would be even nicer if there was an option to delete immediately) :-)

      cheers,

      Rusty

       
    • RustyShelf

      RustyShelf - 2005-06-15

      Oops...ignore that last message. I wasn't setting the properties properly, so mstor thought that the meta data was still enabled!

      I'm still having a few issues, but I'll sort through them to figure out if they are mstor problems (which I think at least one might be), or just things wrong with my code.

      thanks for your help,

      Rusty

       
    • Ulricoher

      Ulricoher - 2005-06-15

      Hi Ben,
      now it works with your advice.
      Thanks a lot & greetings,
      Ulrich

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.