DEV Community

msmittalas
msmittalas

Posted on

Easy to Debug NullPointerException with Java 17

As Java Developer, We have faced NullPointerException throughout
our Java Development career. Sometime ,it is difficult to find such exception at Compile time since its not a checked exception. for example, look at runtime error message in below code.

Image description

Exception Message :
Exception in thread "main" java.lang.NullPointerException
at MyClass.main(MyClass.java:5)

Error message is not clearly informing which variable has an issue.
We need more information on variable causing this exception and that's where JEP-358 comes for rescue.

If We run same code with Java 17 , It provides more information on which variable is causing NullPointerException.

Exception Message :
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "<local2>" is null
at MyClass.main(MyClass.java:5)

NOTE:
This change was introduced in Java 14 but JAVA 14 doesn't have LTS ( long term support)

Happy Coding !!!!

Top comments (0)