Re: clueless newbie -- how to open mbox?

Chris Burdess <[email protected]> Wed, 2 May 2007 14:11:25 +0100
Newsgroups gmane.comp.java.classpath.extensions.javamail
Message-ID <[email protected]>
Matej Cepl wrote:

> Hi, can somebody help me with this:
>
> [matej@chelcicky mdir2mbox]$ cat -b Mdir2mbox.java
>      1  import java.util.*;
>      2  import javax.mail.*;
>
>      3  public class Mdir2mbox {
>      4          public static void main(String[] args) throws \
>      MessagingException {
>      5                  String urlStr = "/home/matej/.eclipse\
> /workspace/Mdir2mbox/examples/test";
>      6                  Properties properties = new Properties();
>      7                  Session session = Session.getInstance 
> (properties);
>      8                  URLName mboxURL = new URLName 
> ("mbox://"+urlStr);
>      9                  Store store = session.getStore(mboxURL);
>     10                  Folder folder = store.getDefaultFolder();
>     11                  if(!folder.exists())
>     12                          folder.create(Folder.HOLDS_MESSAGES);
>     13          folder.open(Folder.READ_WRITE);
>     14                  System.out.println(folder.getMessageCount());
>     15          }
>
>     16  }
> [matej@chelcicky mdir2mbox]$ ls -lh examples/test
> -rw-r--r-- 1 matej matej 22K kvÄ›  1 01:02 examples/test
> [matej@chelcicky mdir2mbox]$ java Mdir2mbox
> Exception in thread "main" java.lang.NullPointerException
>    at Mdir2mbox.main(Mdir2mbox.java:12)

The mbox provider is not really designed to work like that: you  
initialise it with a URL to a directory, and call getFolder to get  
individual mailboxes.

I don't really understand how you can have an NPE at line 12 if you  
don't have one at line 11, but still.

Try it with something like

	Store store = session.getStore(new URLName("mbox:///home/ 
matej/.eclipse/workspace/Mdir2mbox/examples/"));
	Folder test = store.getFolder("test");
	test.open(Folder.READ_WRITE);

If you think the current behaviour is wrong, let me know how you  
think it should work and I'll see what I can do about it.