Why do I get a NullPointer exception when I create an InternetAddress?

Michael Dean

The InternetAddress(String) constructor method will throw a NullPointerException if the String reference passed to it is null.

So, you might ask, why doesn't the documentation list that exception? Well, according to javadoc documentation standards, runtime exceptions (like NullPointerException) should never be listed in the "Throws" section of the javadoc. (Sun did this right. :) Instead, in a case like this--where the runtime exception could occur as a result of a bad parameter value---the documentation (method documentation or "Parameters" section) should specifically mention the possibility. (Sun forgot to do this.)

How can you tell that you'll get a NullPointerException instead of an AddressException? Run this program:

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;

public class Test {
  public static void main(String[] args) {
    try {
      String test = null;
      InternetAddress address = new InternetAddress(test);
      System.out.println(address);
    } catch (MessagingException e) {
      System.err.println(e);
      e.printStackTrace();
    }
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact