jwenjian / ghiblog

GitHub Issues Blog, powered by GitHub Issues and GitHub Actions
https://1link.fun
352 stars 45 forks source link

JEP 358: Helpful NullPointerExceptions #165

Open jwenjian opened 4 years ago

jwenjian commented 4 years ago

Card JEP 358: Helpful NullPointerExceptions added the to the Java/Maven/Spring(boot)/Mybatis list in the 遇见 board at January 7, 2020 at 03:35PM
 
> JDK14的新特性, JVM会提供更有用的空指针异常信息, 即, 会指出是哪个对象的哪个变量为null才导致了空指针异常. The JVM throws a NullPointerException (NPE) at the point in a program where code tries to dereference a null reference. By analyzing the program's bytecode instructions, the JVM will determine precisely which variable was null, and describe the variable (in terms of source code) with a null-detail message in the NPE. The null-detail message will then be shown in the JVM's message, alongside the method, filename, and line number. Note: The JVM displays an exception message on the same line as the exception type, which can result in long lines. For readability in a web browser, this JEP shows the null-detail message on a second line, after the exception type. For example, an NPE from the assignment statement a.i = 99; would generate this message: Exception in thread "main" java.lang.NullPointerException: Cannot assign field "i" because "a" is null at Prog.main(Prog.java:5) If the more complex statement a.b.c.i = 99; throws an NPE, the message would dissect the statement and pinpoint the cause by showing the full access path which led up to the null: Exception in thread "main" java.lang.NullPointerException: Cannot read field "c" because "a.b" is null at Prog.main(Prog.java:5)
 
---
 
View on Trello