JDK-14 Exception Details Feature : Add More Details To Your NullPointerException

JDK-14 provides a new ability to developer for checking root cause of a NullPointerException.
So instead of getting only code line number as a root cause of your NullPointerException, like :
Old NullPointerException
public static void main(String[] args) {
String mystring = null;
System.out.println(mystring.toString());
}
Exception in thread "main" java.lang.NullPointerException
at YourClass.main(your_class.java:5)
From JDK-14 You are can get more details about a NullPointerException by adding the following argument on VM Options :
New VM Argument for more exception details
-XX:+ShowCodeDetailsInExceptionMessages
Result after applying this change :
After adding new ShowCodeDetailsInExceptionMessages argument
public static void main(String[] args) {
String mystring = null;
System.out.println(mystring.toString());
}
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toString()" because "mystring" is null
at YourClass.main(your_class.java:5)
Java/JavaEE (Hybris/Spring) & Full Stack Senior Developer
I’m passionate about new technologies or any kind of thing that change our lifestyle to better either by automatisation of existing process or creating new solution.
Leave a Reply