其他
这几天研究了一下JDK14,发现它处理NPE的方式,真香!
来源:锅外的大佬
String city = employee.getDetailInfos().getRegistryAddress().getCity();
-XX:+ShowCodeDetailsInExceptionMessages
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "RegistryAddress.getCity()" because the return value of
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$DetailInfos.getRegistryAddress()" is null
at com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException.main(HelpfulNullPointerException.java:10)
Cannot invoke "String.toLowerCase()" because the return value of "getEmailAddress()" is null
Employee employee = null;
employee.getName();
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "employee" is null
Cannot invoke
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "<local1>" is null