Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (3.9k points)

I've had instances of our Java code catch a NullPointerException, but when I try to log the StackTrace (which basically ends up calling Throwable.printStackTrace() ), all I get is:

java.lang.NullPointerException

Has anyone else come across this? I tried googling for "java null pointer empty stack trace" but didn't come across anything like this.

1 Answer

0 votes
by (46k points)

You are presumably practicing the HotSpot JVM (originally by Sun Microsystems, later acquired by Oracle, part of the OpenJDK), which delivers a lot of optimization. To get the flue traces back, you need to relinquish the option -XX:-OmitStackTraceInFastThrow to the JVM.

The optimization is that during an exception (typically a NullPointerException) transpires for the first time, the full stack trace is lettered and the JVM remembers the stack trace (or maybe just the location of the code). When that exception transpires often complete, the stack trace is not issued any more, both to achieve better representation and not to flood the log with identical stack traces.

To see how this is performed in the HotSpot JVM, grab a copy of it and search for the global shifting OmitStackTraceInFastThrow. Last time I saw at the code (in 2019), it remained in the file graphKit.cpp.

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...