1
votes

I got this method:

public static boolean isValidEmailAddress(String email) {
       boolean result = true;
       try {
          InternetAddress emailAddr = new InternetAddress(email);
          emailAddr.validate();
       } catch (AddressException ex) {
          result = false;
       }
       return result;
}

but that give me this exception:

java.lang.NullPointerException at javax.mail.internet.AddressParser.tokenizeAddress(AddressParser.java:645) at javax.mail.internet.AddressParser.parseAddress(AddressParser.java:113) at javax.mail.internet.InternetAddress.(InternetAddress.java:70) at javax.mail.internet.InternetAddress.(InternetAddress.java:61) at controller.CtrlUser.isValidEmailAddress(CtrlUser.java:166) at controller.CtrlSeller.register(CtrlSeller.java:41) at testController.CtrlSellerUnitTestCase.testRegister(CtrlSellerUnitTestCase.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) ....

3
are you passing null for email?MK.
Are you sure that during call to this method, parameter email is not null?Aditya Jain
thanks to all of you, it was null ;)Jerome Ansia

3 Answers

5
votes

make sure that the email parameter you are passing is not null

5
votes

It works for me- check to see if you are actually passing a valid string. It appears you are sending null as your email parameter

1
votes

Try following code:

java.net.IDN.toASCII    

public static boolean isValidEmail(String email) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(java.net.IDN.toASCII(email));
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    System.out.println("==> "+result+" : "+email);
    return result;
}