DEV Community

msmittalas
msmittalas

Posted on

4 1

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 !!!!

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay