One of the important features that were introduced with Java SE 14 was the Helpful NullPointerExceptions which is related to the usability of the NullPointerException
. What makes NullPointerException
in Java SE 14 more usable than its predecessor?

- 27,789
- 26
- 218
- 353

- 71,965
- 6
- 74
- 110
-
37[This question is being discussed on Meta](https://meta.stackoverflow.com/q/404681/8967612). – 41686d6564 stands w. Palestine Jan 25 '21 at 03:04
3 Answers
The JVM throws a NullPointerException
at the point in a program where code tries to dereference a null
reference. With Java SE 14, the NullPointerException
offers helpful information about the premature termination of a program. Java SE 14 onwards, the JVM describes the variable (in terms of source code) with a null-detail message in the NullPointerException
. It greatly improves program understanding by more clearly associating a dynamic exception with static program code.
We can see the difference with an example.
import java.util.ArrayList;
import java.util.List;
class Price {
double basePrice;
double tax;
public Price() {
}
public Price(double basePrice) {
this.basePrice = basePrice;
}
public Price(double basePrice, double tax) {
this.basePrice = basePrice;
this.tax = tax;
}
// ...
}
class Product {
String name;
Price price;
public Product() {
}
public Product(String name, Price price) {
this.name = name;
this.price = price;
}
// ...
}
class CartEntry {
Product product;
int quantity;
public CartEntry() {
}
public CartEntry(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
// ...
}
class Cart {
String id;
List<CartEntry> cartEntries;
public Cart() {
cartEntries = new ArrayList<>();
}
public Cart(String id) {
this();
this.id = id;
}
void addToCart(CartEntry entry) {
cartEntries.add(entry);
}
// ...
}
public class Main {
public static void main(String[] args) {
Cart cart = new Cart("XYZ123");
cart.addToCart(new CartEntry());
System.out.println(cart.cartEntries.get(0).product.price.basePrice);
}
}
Output before Java SE 14:
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:74)
This message leaves the programmer clueless about the source of the NullPointerException
.
The output with Java SE 14 onwards:
Exception in thread "main" java.lang.NullPointerException: Cannot read field "price" because "java.util.List.get(int).product" is null
at Main.main(Main.java:74)
The NullPointerException
in Java SE 14 also tells us which reference is null
.
A great improvement!

- 30,714
- 6
- 20
- 43

- 71,965
- 6
- 74
- 110
-
3The "premature termination" isn't relevant. The information is still there if you catch it instead. – OrangeDog Jan 25 '21 at 16:04
-
4It is vague and inaccurate to imply that exceptions cause "premature termination of the program". Yes, *uncaught* exceptions cause the *thread* to complete abruptly, but that only terminates the program if it was the last non-daemon thread alive. And even if the program chooses to terminate, this doesn't imply a lack of maturity. – meriton Jan 25 '21 at 18:51
-
3
-
1This is a nice answer and could be complemented with the infor from the other ones related to newer java versions. – lucrib Apr 21 '22 at 12:09
It is documented in the release notes.
The new message is not being displayed by default in version 1.14:
A new option is available to provide more helpful NullPointerException messages:
-XX:+ShowCodeDetailsInExceptionMessages
If the option is set, on encountering a null pointer, the JVM analyzes the program to determine which reference was null and then provides the details as part of NullPointerException.getMessage(). In addition to the exception message, the method, filename, and line number are also returned.
By default, this option is disabled.
and the full proposal JEP 358 for motivation.
Eventually
The default of the flag ShowCodeDetailsInExceptionMessages was changed to 'true'.

- 48,631
- 24
- 141
- 189
When the JVM throws a NullPointerException
, it now adds a detail message that specifies which reference was null.
This makes the stack trace easier to interpret, and resolves ambiguity if the program accesses several references on the same line in the source code. For instance, the line
person.name = student.name;
throws a NullPointerException
if person
or student
is null
, but prior to Java 14, the exception didn't tell us which one it was. Now, it does:
java.lang.NullPointerException: Cannot read field "name" because "student" is null
Further information about this change is available in JEP-358.

- 68,356
- 14
- 108
- 175