Improved Java NullPointerExceptions – JEP 358 , Since Java 14.

Since Java 14, Java improved the usability of NullPointerExceptions generated by the JVM by describing precisely which variable was null.

Before Java 14, the error messages for NullPointerExceptions look like below. It only gives the File name and line number.

Exception in thread "main" java.lang.NullPointerException
at com.jsession4d.NullPointerDemo.checkState(NullPointerDemo.java:24)
at com.jsession4d.NullPointerDemo.main(NullPointerDemo.java:18)

From Java 14, as part of JEP(JDK Enhancement Proposal) 358, the null pointer exception messages were enhanced to describe precisely which variable was null. For example consider the below code.

1. package com.jsession4d;
2. import lombok.AllArgsConstructor;
3. import lombok.Data;
4.
5. public class NullPointerDemo {
6.   public static void main(String args[]){
7.      Customer customer1 = new Customer(1, "Luke", new Address("Line1", "City","CA","USA"));
8.      Customer customer2 = new Customer(2, "Ryan", null);
9.      Customer customer3 = new Customer(3, "Mike", new Address("Line1", "City",null,"USA"));
10.
11.     checkState(null, customer1, customer2 ); //Customer is null
12.     checkState(customer2);  //Customer Address is null
13.     checkState(customer3);  //Customer.Address.State is null
14.  }
15.
16.  public static String checkState(Customer... customers){
17.      for(Customer customer : customers){
18.          if(customer.getName()!= null && customer.getAddress().getState().equalsIgnoreCase("CA")){
19.              return customer.getName();
20.          }
21.      }
22.        return null;
23.    }
24.
25. }
26. 
27. @Data
28. @AllArgsConstructor
29. class Customer{
30.    Integer id;
31.    String name;
32.    Address address;
33. }
34. 
35. @Data
36. @AllArgsConstructor
37. class Address{
38.  String Line;
39.  String City;
40.  String State;
41.    String Country;
42. }

All 3 checkState calls in line numbers 11, 12 & 13 will create NullPointerException in line number 18: if(customer.getName()!= null && customer.getAddress().getState().equalsIgnoreCase("CA")){

Before Java 14, all three calls will return the same error message as shown below. From this error message it is difficult to find out which field is null. It could be customer, or customer.address or customer.address.state

Exception in thread "main" java.lang.NullPointerException
at com.jsession4d.NullPointerDemo.checkState(NullPointerDemo.java:18)
at com.jsession4d.NullPointerDemo.main(NullPointerDemo.java:11)

From Java 14, the messages have been enhanced to show what is causing the NullPointerException and why (as shown below).

For checkState(null, customer1, customer2 ) call in line number 11, below is the new enhanced error message. It clearly says Customer.getName() in line number 18 is causing the NullPointerException because Customer object is null.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.jsession4d.Customer.getName()" because "customer" is null
at com.jsession4d.NullPointerDemo.checkState(NullPointerDemo.java:18)
at com.jsession4d.NullPointerDemo.main(NullPointerDemo.java:11)

For checkState(customer2); call in line number 12, below is the new enhanced error message.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.jsession4d.Address.getState()" because the return value of "com.jsession4d.Customer.getAddress()" is null
at com.jsession4d.NullPointerDemo.checkState(NullPointerDemo.java:18)
at com.jsession4d.NullPointerDemo.main(NullPointerDemo.java:12)

For checkState(customer3); call in line number 13, below is the new enhanced error message. In addition to the File name and line number, it also provides details about what is causing NullPointerException and why.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equalsIgnoreCase(String)" because the return value of "com.jsession4d.Address.getState()" is null
at com.jsession4d.NullPointerDemo.checkState(NullPointerDemo.java:18)
at com.jsession4d.NullPointerDemo.main(NullPointerDemo.java:13)

%d bloggers like this: