logo

Helpful NullPointerException


Show

Java 14 released NullPointerException with many important pieces of information in case the -XX:+ShowCodeDetailsInExceptionMessages flag proceeds to JVM.

Example

Examine the below given example:

ApiTester.java

public class APITester {
   public 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)