- Java 14 Tutorial
- Java 14 - Home
- Java 14 - Overview
- Java 14 - Environment Setup
- Java 14 Language Changes
- Java 14 - Switch Expressions
- Java 14 - Text Blocks
- Java 14 - pattern for instanceOf
- Java 14 - NullPointerException
- Java 14 JVM Changes
- Java 14 - Packaging Tools
- Java 14 - NUMA Aware G1
- Java 14 - Others
- Java 14 - Deprecation & Removals
- Java 14 Useful Resources
- Java 14 - Quick Guide
- Java 14 - Useful Resources
- Java 14 - Discussion
Java 14 - 有用的 NullPointerException
Java 14 引入了 NullPointerException,其中包含有用的信息,以防 -XX:+ShowCodeDetailsInExceptionMessages 标志传递到 JVM。
例子
考虑以下示例 -
ApiTester.java
public class APITester { public static void main(String[] args) { String message = null; System.out.println(message.length()); } }
老方法:编译并运行程序
$javac APITester.java $java APITester
输出
Exception in thread "main" java.lang.NullPointerException at APITester.main(APITester.java:6)
新方式:使用新标志编译并运行程序
$javac APITester.java $java -XX:+ShowCodeDetailsInExceptionMessages APITester
输出
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null at APITester.main(APITester.java:6)