A NullPointerException is thrown by an object when the method of an object is called, where the object is null. Before the Java 14 version,

It throws an error, and errors contain the line number on which an exception is thrown at runtime.

Let’s see an example to reproduce the NullPointerException.

package java14;

public class JavaNullTest {
    public static void main(String[] args) {
        Employee emp = new Employee();
        System.out.println(emp.department.name); // emp.department is null, throws an error
    }
}
class Employee {
    Department department;
    String name;
}

class Department {
    String name;
}

Outputs:

Exception in thread "main" java.lang.NullPointerException
 at HelloWorld.main(HelloWorld.java:7)

NullPointerException is thrown by the calling method or accessing property on a null object.

Java 14 introduced an improvement to a user in how the error message is displayed.

java14 NullPointerException enhancement

In Java14, improves how the error message is displayed to a user.

It gives the following error while accessing the property on a null object.

Let’s see a use case for enhancing of error message.

package java14;

public class Java14NullTest {
    public static void main(String[] args) {
        Employee emp = new Employee();
        System.out.println(emp.department.getName());
    }
}
class Employee {
    Department department;
    String name;
}
class Department {
    String name;
    String getName(){
        return name;
    }
}
  • reading property on a null object

emp.department.name throws the below error as the department is null.

The error message calling property on Null object.

Exception in thread "main" java.lang.NullPointerException: Cannot read field "name" because "emp.department" is null
 at java14.JavaNullTest.main(JavaNullTest.java:6)
  • calling method on Null Object

let’s see an example of calling a method on a null object

calling emp.department.getName() method, throws below error.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java14.Department.getName()" because "emp.department" is null
 at java14.Java14NullTest.main(Java14NullTest.java:6)

Here department is null, and calling the getName() method throws this error.

  • Assign a value to a null object property

In this example, You are assigning a string to a property of a null object and throwing the below error.

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "name" because "emp.department" is null
 at java14.Java14NullTest.main(Java14NullTest.java:6)

here is an example

package java14;

public class Java14NullTest {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.department.name="john";
    }
}
class Employee {
    Department department;
    String name;
}
class Department {
    String name;
    String getName(){
        return name;
    }
}

This feature is not enabled by default, You have to pass command line options -XX:+ShowCodeDetailsInExceptionMessages to display this type of error at runtime.