Java14 处理 NullPointerException 新方式,真的太香了!
共 3080字,需浏览 7分钟
·
2021-12-01 01:19
阅读本文大概需要 3.5 分钟。
来自:https://mp.weixin.qq.com/s/zizw87et1ItE5tJWygKx-w
1.传统的 NullPointerException
String city = employee.getDetailInfos().getRegistryAddress().getCity();
employee
, getDetailInfos()
,或者 getRegistryAddress()
为空,JVM就会抛出 NullPointerException
,
那么导致异常的根本原因是什么?如果不使用调试器,很难确定哪个变量为空。而且,JVM也只会打印导致异常的方法、文件名和行号,仅此而已。那么下面,我将带大家了解Java 14如何通过 JEP358
解决这个问题。2.增强型 NullPointerException
-XX:+ShowCodeDetailsInExceptionMessages
2.1 详细的异常信息
ShowCodeDetailsInExceptionMessages
标志的情况下再次运行代码:
Exceptionin thread "main" java.lang.NullPointerException:
Cannot invoke "RegistryAddress.getCity()" because the return value of
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$DetailInfos.getRegistryAddress()"isnull
at com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException.main(HelpfulNullPointerException.java:10)
Cannot invoke "String.toLowerCase()" because the return value of "getEmailAddress()"isnull
3. 技术方面
Employee employee = null;
employee.getName();
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "employee"isnull
Cannot invoke
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "
" isnull
推荐阅读:
面试官问:Mybatis Plus 是如何实现动态 SQL 语句的?原理你懂吗?
内容包含Java基础、JavaWeb、MySQL性能优化、JVM、锁、百万并发、消息队列、高性能缓存、反射、Spring全家桶原理、微服务、Zookeeper、数据结构、限流熔断降级......等技术栈!
⬇戳阅读原文领取! 朕已阅