English 中文(简体)
Java 14 - NullPointerException
  • 时间:2024-09-17

Java 14 - Helpful NullPointerException


Previous Page Next Page  

Java 14 introduces NullPointerException with helpful information in case -XX:+ShowCodeDetailsInExceptionMessages flag is passed to JVM.

Example

Consider the following example −

ApiTester.java


pubpc class APITester {
   pubpc static void main(String[] args) {
      String message = null;
      System.out.println(message.length());
   }   
}

Old Way: Compile and Run the program


$javac APITester.java
$java APITester

Output


Exception in thread "main" java.lang.NullPointerException
   at APITester.main(APITester.java:6)

New Way: Compile and Run the program with new Flag


$javac APITester.java
$java -XX:+ShowCodeDetailsInExceptionMessages APITester

Output


Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null
   at APITester.main(APITester.java:6)
Advertisements