JDK 14中更好的NPE消息

我的2019年3月博客文章“ Java会出现更好的默认NullPointerException消息吗? ”是在尚未针对特定JDK版本针对更好的NullPointerException消息的JEP 草案编写时编写的。 此后,该JEP草案成为JEP 14的 目标 JEP 358(“ Helpful NullPointerExceptions”) 。 更好的是,初始实现( JDK-8218628 ) 已经在JDK 14分支中 ,并且可以在JDK 14 Early Access Build Build 20(2019/10/23)中使用

在本文中,我将针对JDK 14 Early Access Build 20运行先前文章中介绍的示例代码 ,以演示现在提供的其他详细信息。 要查看为故意引入导致NullPointerException的各种情况而编写的示例代码,请参阅前面的文章或查看GitHub上的源代码 。

下载JDK 14 Early Access Build 20并按其路径指向该文件后,运行java -version时会看到以下内容:

 openjdk version "14-ea" 2020 - 03 - 17  OpenJDK Runtime Environment (build 14 -ea+ 20 - 879 )  OpenJDK 64 -Bit Server VM (build 14 -ea+ 20 - 879 , mixed mode, sharing) 

在正确配置了JDK 14 Early Access Build 20之后 ,我重新构建了前面提到的源代码 ,然后使用Java启动器重新运行了该代码,而没有任何新选项。 该输出(如下所示)与以前的JDK版本的输出没有实质性差异。

 =========================================  | # | # 1 : Element [ null boolean array | ] on : Element [ 0 ] on  =========================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateFirstExampleIndexAccessOnNullBooleanArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =================================  | # | # 2 : .length on null boolean [] |  =================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateSecondExampleLengthOnNullBooleanArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =======================================  | # | # 3 : Assigning float : Assigning to null float [] |  =======================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateThirdExampleAssigningValueToElementOfNullFloatArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ======================================  | # | # 4 : Accessing field on null object | : Accessing field on object |  ======================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateFourthExampleAccessInstanceFieldOfNullObject(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ===================  | # | # 5 : throw null ; | ; |  ===================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateFifthExampleThrowingConstantNull(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ================================================  | # | # 6 : Method invocation on null instance field | : Method invocation on  ================================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateSixthExampleMethodInvocationOnNullInstanceField(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =============================================  | # | # 7 : () on null instance field | () on synchronized () on instance field |  =============================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateSeventhExampleSynchronizedNullInstanceField(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ==========================================================================  | >>> Null Lost in Long Series of Method Invocations in Single Statement |  ==========================================================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateNullLostInSeriesOfMethodInvocationsInSingleStatement(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =======================================================  | >>> Null Lost in Dereferenced Constructor Arguments |  =======================================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateNullLostInConstructorAcceptingMultiplePotentiallyNullArgumentsDereferenced(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ==================================================  | >>> Null Lost in Dereferenced Method Arguments |  ==================================================  java.lang.NullPointerException at dustin.examples.npe.NpeDemo.demonstrateNullLostInMethodAcceptingMultiplePotentiallyNullArgumentsDereferenced(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source) 

如以上输出所示,即使使用新的JDK 14 Early Access Build 20,当我正常运行应用程序时,也看不到有关NullPointerException的任何新的详细信息。 我包含了此输出,以表明需要一个特殊的标志来启用更详细的NullPointerException ,并使其更方便地比较带有和不带有其他详细信息的输出。 下一个输出清单显示了将Java启动器传递给标志-XX:+ShowCodeDetailsInExceptionMessages时提供的其他详细信息:

 =========================================  | # | # 1 : Element [ null boolean array | ] on : Element [ 0 ] on  =========================================  java.lang.NullPointerException: Cannot load from byte / boolean array because java.lang.NullPointerException: Cannot load from array because "<local1>" is null at dustin.examples.npe.NpeDemo.demonstrateFirstExampleIndexAccessOnNullBooleanArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =================================  | # | # 2 : .length on null boolean [] |  =================================  java.lang.NullPointerException: Cannot read the array length because "<local1>" is null at dustin.examples.npe.NpeDemo.demonstrateSecondExampleLengthOnNullBooleanArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =======================================  | # | # 3 : Assigning float : Assigning to null float [] |  =======================================  java.lang.NullPointerException: Cannot store to array because "<local1>" is null array because java.lang.NullPointerException: Cannot store to float array because at dustin.examples.npe.NpeDemo.demonstrateThirdExampleAssigningValueToElementOfNullFloatArray(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ======================================  | # | # 4 : Accessing field on null object | : Accessing field on object |  ======================================  java.lang.NullPointerException: Cannot read field "nullInstanceField" because "<local1>" is null at dustin.examples.npe.NpeDemo.demonstrateFourthExampleAccessInstanceFieldOfNullObject(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ===================  | # | # 5 : throw null ; | ; |  ===================  java.lang.NullPointerException: Cannot throw exception because "null" is null at dustin.examples.npe.NpeDemo.demonstrateFifthExampleThrowingConstantNull(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ================================================  | # | # 6 : Method invocation on null instance field | : Method invocation on  ================================================  java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "this.nullInstanceField" is null at dustin.examples.npe.NpeDemo.demonstrateSixthExampleMethodInvocationOnNullInstanceField(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =============================================  | # | # 7 : () on null instance field | () on synchronized () on instance field |  =============================================  java.lang.NullPointerException: Cannot enter synchronized block because "this.nullInstanceField" is null at dustin.examples.npe.NpeDemo.demonstrateSeventhExampleSynchronizedNullInstanceField(Unknown Source) at dustin.examples.npe.NpeDemo.demonstrateJdk8218628Examples(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ==========================================================================  | >>> Null Lost in Long Series of Method Invocations in Single Statement |  ==========================================================================  java.lang.NullPointerException: Cannot invoke "dustin.examples.npe.DysfunctionalLocation$Province.getCity()" because the return value of "dustin.examples.npe.DysfunctionalLocation$Nation.getProvince()" is null at dustin.examples.npe.NpeDemo.demonstrateNullLostInSeriesOfMethodInvocationsInSingleStatement(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  =======================================================  | >>> Null Lost in Dereferenced Constructor Arguments |  =======================================================  java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because "<local6>" is null at dustin.examples.npe.NpeDemo.demonstrateNullLostInConstructorAcceptingMultiplePotentiallyNullArgumentsDereferenced(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source)  ==================================================  | >>> Null Lost in Dereferenced Method Arguments |  ==================================================  java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because "<local6>" is null at dustin.examples.npe.NpeDemo.demonstrateNullLostInMethodAcceptingMultiplePotentiallyNullArgumentsDereferenced(Unknown Source) at dustin.examples.npe.NpeDemo.main(Unknown Source) 

JEP 358解释了使用此标志来查看其他NullPointerException详细信息:“可以使用新的boolean命令行选项-XX { + | - } ShowCodeDetailsInExceptionMessages -XX { + | - } ShowCodeDetailsInExceptionMessages 。 该选项将首先具有默认值' false ',以便不打印消息。 它旨在在以后的版本中默认情况下在异常消息中启用代码详细信息。” 如我们所见,此功能默认情况下最初处于关闭状态,但是有计划在将来启用更详细的NullPointerException消息。

最近的一条Tweet问了一个问题:“如果字节码不包含变量名,它将如何工作?” 这个问题通过提供一个具体的例子来继续:“假设我们有像Object a = ....; a.getName(); //NPE这样的代码Object a = ....; a.getName(); //NPE Object a = ....; a.getName(); //NPE Object a = ....; a.getName(); //NPE NPE会Object a = ....; a.getName(); //NPE哪种消息?” 尽管在前面显示的一系列测试中都包含了一个示例,但我认为我将在此提供一个针对性更强的示例来回答该问题。 下一个代码清单(也在GitHub上提供 )显示了改编自Tweet中使用的示例的代码。

 package dustin.examples.npe;  /** * Simple demonstration to answer Tweet-ed question * "How it will work if bytecode doesn't contain variable names?" * ( https://twitter.com/2doublewhiskey/status/1180365953240055809 ). */  public class TwoDoubleWhiskeyTweetExample  { public static void main( final String[] arguments) { final Person person = null ; person.getName(); //NPE }    public static class Person { private String name; public Person( final String newName) { name = newName; } public String getName() { return name; } }  } 

下一个屏幕快照显示了使用JDK 14 Early Access Build 20(不带java启动器标志-XX:+ShowCodeDetailsInExceptionMessages运行此简单应用程序的结果。

如屏幕快照所示,在JDK 14 Early Access Build 20中使用-XX:+ShowCodeDetailsInExceptionMessages标志可提供与此简单NullPointerException示例相关的其他详细信息:“无法调用” dustin.examples.npe.TwoDoubleWhiskeyTweetExample $ Person.getName() ”,因为“ <local1> ”为空”

GitHub上提供了一个更简单甚至更接近Tweet-ed问题中提供的原始示例的示例。

JEP 358 (“ 有用的NullPointerExceptions ”)可能不像新JDK发行版中的其他一些JEP那样浮华,但最终可能是每天为Java开发人员提供比其一些浮华同行更高的价值 。 有许多示例将对您有所帮助,JEP本身以及本文引用的我的代码示例中都阐明了许多示例情况。

翻译自: https://www.javacodegeeks.com/2019/10/better-npe-messages-in-jdk-14.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/341036.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jvm能识别什么字符集_识别JVM –比预期要难

jvm能识别什么字符集在Plumbr&#xff0c;我们花费了上个月的时间来为将来的重大改进奠定基础。 此类构件之一是为JVM添加唯一标识符&#xff0c;以便将来自同一JVM的所有会话链接在一起。 尽管一开始似乎是一项琐碎的任务&#xff0c;但是当查看JVM捆绑的jps命令的输出时&…

Spring MVC绑定,无设置器

即使域模型对象没有设置器&#xff0c;也可以将表单参数绑定到域模型对象。 只需添加带有InitBinder方法的ControllerAdvice类&#xff0c;即可通过initDirectFieldAccess&#xff08;&#xff09;方法将应用程序配置为进行字段绑定 package boottests.controllers;import org…

idf和adf_ADF:弹出窗口,对话框和输入组件

idf和adf在本文中&#xff0c;当我们有一个af&#xff1a;popup包含af&#xff1a;dialog并在其中包含输入组件时&#xff0c;我想重点介绍一个非常常见的用例。 实现此用例时&#xff0c;需要注意一些陷阱。 让我们考虑一个简单的示例&#xff1a; <af:popup id"p1&…

自制串口示波器小工具

因为博主喜欢玩嵌入式&#xff0c;经常与各种硬件打交道&#xff0c;常常需要采集下位机的信号&#xff0c;无奈学生党&#xff0c;买不起示波器&#xff0c;自己用python写了一个串口示波器的上位机&#xff0c;可以很简单的和下位机配合组成示波器&#xff0c;效果如图&#…

ELK堆栈入门

朋友不允许朋友写用户身份验证。 厌倦了管理自己的用户&#xff1f; 立即尝试Okta的API和Java SDK。 在几分钟之内即可对任何应用程序中的用户进行身份验证&#xff0c;管理和保护。 好的设计原则要求微服务架构是可观察的&#xff0c;并提供集中的监视工具。 该工具使开发团队…

speex在stm32f407单片机上的移植

最近做的网络语音的项目需要用到speex中的一些功能&#xff0c;查了一下资料发现移植给mcu的经验大多零零碎碎&#xff0c;自己捣鼓了一晚上总算是移植好了&#xff0c;写个博客记录一下。1.获取speex源码 官方下载&#xff1a; 官方地址 博主移植用的源码以及移植好的工程下…

在Kafka上异步发送数据

对于一个项目&#xff0c;我试图记录用户的基本交易&#xff0c;例如添加和删除一个项目以及多种类型的项目&#xff0c;并为每笔交易向kafka发送一条消息。 日志机制的准确性不是至关重要的&#xff0c;在kafka服务器停机的情况下&#xff0c;我不希望它阻止我的业务代码。 在…

devc++工程提示“源文件未编译”的可能问题

博主使用devc5.11&#xff0c;win7&#xff0c;创建了一个c工程以后编译一直提示“源文件未编译”&#xff0c;查了两小时博客期间反复重装&#xff0c;最后还是靠自己误打误撞解决了&#xff0c;解决步骤如下&#xff1a; 1、安装时语言选择English。 网上各种教程教新人安装…

vmware网络桥接模式无法上网的解决办法

1.vmware->编辑->虚拟网络编辑器->桥接模式->选择有线网卡 2.VMware-虚拟机设置-网络适配器-桥接模式-复制物理网络连接状态、启动时连接 3.重启虚拟机&#xff0c;完成 如果出现连接到网络但是无法上网的情况&#xff0c;还需做如下处理 1.sudo gedit /etc/res…

oracle idm_深入了解Oracle IDM审核

oracle idm在处理敏感信息的任何产品中&#xff0c; 报告都是至关重要的功能。 同样适用于身份和访问管理工具。 Oracle IDM的审核模块是其OOTB报告功能的基础。 让我们快速了解一下审核引擎以及它如何促进OIM中的报告功能。 这里介绍的用例很简单– 在OIM中更改为用户记录。 …

C语言编写贪吃蛇游戏

自己用C语言编写一个贪吃蛇游戏&#xff0c;效果如图&#xff1a; 源代码可免费下载&#xff0c;传送门如下&#xff1a; 点击下载贪吃蛇游戏和源代码

JDK 13中的JEP 355文本块

JDK 13已于2019年9月17日上线GA&#xff0c; 此处列出了重要的新功能。 新功能之一是“文本块”。 这样可以轻松地编写多行字符串&#xff0c;而不必在拆分成不同的行时进行串联。 让我们快速了解创建多行字符串的不同方法&#xff1a; String aBlock """ SE…

java8 默认方法_默认方法:Java 8的无名英雄

java8 默认方法几周前&#xff0c;我写了一个博客&#xff0c;说开发人员学习新语言是因为它们很酷。 我仍然坚持这个主张&#xff0c;因为关于Java 8的事情真的很酷。 尽管毫无疑问&#xff0c;该节目的明星是添加了Lambdas和将函数提升为一等变量&#xff0c;但我目前最喜欢的…

两个常见的并发错误

作为Baeldung的编辑&#xff0c;我很高兴与一位作者一起撰写有关Java通用并发陷阱的文章。 这是一本不错的书&#xff0c;但是假设开发人员具有一定的能力。 我已经看到了几件即时并发失败的事情。 它们很容易添加到代码中&#xff0c;并保证为您提供奇怪的结果。 开发人员仍会…

kotlin自定义View出现 java.lang.ClassNotFoundException

问题1&#xff1a;找不到所引用的自定义View Didn’t find class “dxf.example.dxf.customviewdemo.MyTextView” on path: DexPathList 原因&#xff1a;build.gradle中 应用插件 解决&#xff1a;添加 apply plugin: ‘kotlin-android’ 问题2&#xff1a; java.lang.Cl…

javafx swing_JavaFX技巧9:请勿混用Swing / JavaFX

javafx swingJavaFX团队非常努力地说服我们&#xff0c;因为可以将Swing内容嵌入JavaFX UI中&#xff0c;反之亦然&#xff0c;因此从Swing迁移到JavaFX很容易。 我必须承认&#xff0c;我从来没有尝试过&#xff0c;但是根据我从客户那里得到的反馈&#xff0c;我只能建议不要…

kotlin-unresolved reference daclaredFunctions

问题&#xff1a;如题 原因&#xff1a; 默认编译时不导入kotlin-reflect.jar包导致&#xff0c;所以在该包中的默认不能使用 解决 需额外在dependencies中添加 kotlin-reflect的编译&#xff0c; compile “org.jetbrains.kotlin:kotlin-reflect:$kotlin-version”

AWS Loft的数据库周

这是我的笔记&#xff1a; https://databaseweekoctober2019sf.splashthat.com AWS上的数据库&#xff1a;正确工作的正确工具 在许多此类谈话中&#xff0c;我并没有做过深刻的记录。 我正在关注重点。 PostgreSQL排在MySQL之后。 AWS上8种类型的数据库&#xff1a; 关系…

MockWebServer[45678] connection from null failed: java.net.SocketException

MockWebServer使用中的异常 MockWebServer: MockWebServer[45678] connection from null failed: java.net.SocketException: sendto failed: EBADF (Bad file number) java.io.IOException: unexpected end of stream on Connection Caused by: java.io.EOFException: \n n…

ExternalDocumentationLinkImpl(url=https://developer.android.com/reference/, packageListUrl=https://d

dokka 问题 java.lang.RuntimeException: Exception while loading package-list from ExternalDocumentationLinkImpl(urlhttps://developer.android.com/reference/, packageListUrlhttps://developer.android.com/reference/package-list) 原因 使用了下面这个任务定义…