Close

Java 14 - Helpful NullPointerException

[Updated: Mar 20, 2020, Created: Mar 19, 2020]

Java 14 enhances NullPointerException by showing precisely which variable was null.

Consider chain of variables a.b.c.d given that a.b is null.

Before Java 14
a.b.c.d = "test";
Exception in thread "main" java.lang.NullPointerException
In Java 14
Exception in thread "main" java.lang.NullPointerException: Cannot read field "c" because "a.b" is null

This feature can be enabled with the new boolean command-line option:
-XX:+ShowCodeDetailsInExceptionMessages
By default it is disabled i.e.
-XX:-ShowCodeDetailsInExceptionMessages.
It is intended to be enabled by default in a later release.

Java 14 IDE support: IntelliJ IDEA 2020.1 (early access)

Example

package com.logicbig.example;

public class Person {
  Name name ;
    .............
}
package com.logicbig.example;

public class Name {
  String firstName;
  String lastName;
    .............
}
package com.logicbig.example;

public class Example1 {
  public static void main(String[] args) {
      Person person = new Person();
      System.out.println(person.name.firstName);
  }
}
java.lang.NullPointerException: Cannot read field "firstName" because "person.name" is null
at com.logicbig.example.Example1.main(Example1.java:6)

Make sure you have set VM option -XX:+ShowCodeDetailsInExceptionMessages as shown:

If person is null itself:

package com.logicbig.example;

public class Example2 {
  public static void main(String[] args) {
      Person person = null;
      System.out.println(person.name.firstName);
  }
}
java.lang.NullPointerException: Cannot read field "name" because "person" is null
at com.logicbig.example.Example2.main(Example2.java:6)

Using Getters

package com.logicbig.example;

public class Example3 {
  public static void main(String[] args) {
      Person person = new Person();
      System.out.println(person.getName().getFirstName());
  }
}
java.lang.NullPointerException: Cannot invoke "com.logicbig.example.Name.getFirstName()" because the return value of "com.logicbig.example.Person.getName()" is null
at com.logicbig.example.Example3.main(Example3.java:6)

Assignment

package com.logicbig.example;

public class Example4 {
  public static void main(String[] args) {
      Person person = new Person();
      person.name.firstName =  "Jenny";
  }
}
java.lang.NullPointerException: Cannot assign field "firstName" because "person.name" is null
at com.logicbig.example.Example4.main(Example4.java:6)

Multidimensional array

package com.logicbig.example;

public class Example5 {
  public static void main(String[] args) {
      String[][][] data = new String[1][1][];
      data[0][0][0] = "jenny";
  }
}
java.lang.NullPointerException: Cannot store to object array because "data[0][0]" is null
at com.logicbig.example.Example5.main(Example5.java:6)

Example Project

Dependencies and Technologies Used:

  • JDK 14
JEP 358: Helpful NullPointerException Select All Download
  • java-14-null-pointer-exception-example
    • src
      • com
        • logicbig
          • example
            • Example1.java

    See Also