0 votes
1 view
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 (41k 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...