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 :

[pastacode lang=”java” manual=”public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20String%20mystring%20%3D%20null%3B%0A%0A%20%20%20%20%20%20%20%20System.out.println(mystring.toString())%3B%0A%20%20%20%20%7D%0A%0A%0AException%20in%20thread%20%22main%22%20java.lang.NullPointerException%0A%09at%20YourClass.main(your_class.java%3A5)” message=”Old NullPointerException” highlight=”” provider=”manual”/]

From JDK-14 You are can get more details about a NullPointerException by adding the following argument on VM Options :

[pastacode lang=”java” manual=”-XX%3A%2BShowCodeDetailsInExceptionMessages” message=”New VM Argument for more exception details” highlight=”” provider=”manual”/]

Result after applying this change :

[pastacode lang=”markup” manual=”public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20String%20mystring%20%3D%20null%3B%0A%0A%20%20%20%20%20%20%20%20System.out.println(mystring.toString())%3B%0A%20%20%20%20%7D%0A%0A%0AException%20in%20thread%20%22main%22%20java.lang.NullPointerException%3A%20Cannot%20invoke%20%22String.toString()%22%20because%20%22mystring%22%20is%20null%0A%09at%20YourClass.main(your_class.java%3A5)” message=”After adding new ShowCodeDetailsInExceptionMessages argument” highlight=”” provider=”manual”/]

0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x