我有这个方法:
public static boolean isValidEmailAddress(String email) {
boolean result = true;
try {
InternetAddress emailAddr = new InternetAddress(email);
emailAddr.validate();
} catch (AddressException ex) {
result = false;
}
return result;
}
但是这给了我这个例外:
java.lang.NullPointerException at javax.mail.internet.AddressParser.tokenizeAddress(AddressParser.java:645) 在 javax.mail.internet.AddressParser.parseAddress(AddressParser.java:113) 在 javax.mail.internet.InternetAddress。(InternetAddress.java:70) 在 javax.mail.internet.InternetAddress。(InternetAddress.java:61) 在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) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 在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)....
答案 0 :(得分:4)
它适用于我 - 检查你是否实际传递了一个有效的字符串。您似乎发送了null作为电子邮件参数
答案 1 :(得分:4)
确保您传递的电子邮件参数不为空
答案 2 :(得分:1)
请尝试以下代码:
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;
}