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

Java featured image

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)

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of