This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

This article, the original problem: translate for stackoverflow question stackoverflow.com/questions/2…

Issue an overview

I already have an instance, but we will capture a NullPointerException Java code or error, but when I try to record down the stack trace information (is called Throwable. PrintStackTrace () but I get the following information.

java.lang.NullPointerException
Copy the code

Only these

Has anyone else had the same thing happen to me? I tried Googling “Java NULL Pointer Empty Stack Trace”. But no one, no one is like me.

Best answer

You’re probably using HotSpot JVM (originally by Sun Microsystems and later acquired by Oracle, again part of OpenJDK), which performs a lot of optimizations. To obtain a stack trace, you need to option – XX: – OmitStackTraceInFastThrow passed to the JVM

There is an optimization for the first time an exception (especially a NullPointerException) is encountered. The entire stack trace is printed, and the JVM remembers the stack trace information (or perhaps only the code location). When exceptions occur frequently enough. Stack trace information will not be printed again. It also improves performance, rather than flooding the output with the same stack trace log information.

To see how to implement this functionality in the HotSpot JVM, please obtain a copy of it and search for global variable OmitStackTraceInFastThrow. The last time I looked at the code (in 2019), it was in the graphKit.cpp file. A copy of the JVM hg.openjdk.java.net/jdk/jdk/ graphKit. CPP hg.openjdk.java.net/jdk/jdk/fil…