Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

Production logs show NullPointerException with no stack trace and no message after the service has been running for a while, while early occurrences had full traces. Explain.

This is HotSpot's OmitStackTraceInFastThrow optimization. When compiled code throws the same kind of implicit exception, such as NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException or ClassCastException, often enough at one place, the JIT recompiles that code to throw a preallocated instance with no stack trace and no message. It is on by default and can be turned off with -XX:-OmitStackTraceInFastThrow; meanwhile, the first occurrences in earlier logs still have full traces. The real issue is the frequent NPE itself, which is a bug to fix.

Why this question is asked

Few things are more frustrating on call than a log full of java.lang.NullPointerException with nothing after it: no message, no line number, no stack trace. The natural assumption is that the logging is broken, and teams spend hours checking encoders and log levels. Interviewers ask this because the explanation sits in the JIT, and knowing it turns a long investigation into a five-minute one. In this answer, we will recap implicit exceptions, see what the optimization does and why, find the original trace, turn the optimization off where that helps, and fix the actual bug.

1A Quick Recap of Implicit Exceptions

Most exceptions are created explicitly by code with throw new SomeException(...). A few are created by the JVM itself when an instruction fails: dereferencing null throws NullPointerException, integer division by zero throws ArithmeticException, an array index out of range throws ArrayIndexOutOfBoundsException, storing the wrong type into an array throws ArrayStoreException, and a failed cast throws ClassCastException. These are called implicit exceptions, because no throw statement appears in our code.

Normally, the JVM creates a fresh exception object each time, with a stack trace and, for NullPointerException since Java 15, a helpful message describing exactly what was null. That is the full trace the early log entries showed.

2What OmitStackTraceInFastThrow Does

Creating an exception with a stack trace is expensive, mostly because fillInStackTrace walks the whole stack. If compiled code throws the same implicit exception at the same place again and again, the JIT considers it a hot throw. When it recompiles that code, it replaces the normal exception creation with a throw of a preallocated exception object that the JVM keeps for that exception type. That object has no stack trace and no message, and it is shared by every such throw.

The flag that controls this is OmitStackTraceInFastThrow, and it is enabled by default in HotSpot. It only affects C2-compiled code, after the code has become hot and the throw has happened enough times, which is why the change in the logs comes after the service has been running a while, not at startup.

Java
// After a while in production, the same bug starts to look like this:
2026-11-04T10:15:02 ERROR OrderController Request failed
java.lang.NullPointerException: Cannot invoke "String.length()" because the return value of "com.acme.Customer.getEmail()" is null
at com.acme.EmailValidator.check(EmailValidator.java:18)
at com.acme.OrderService.place(OrderService.java:64)
...
2026-11-04T14:47:31 ERROR OrderController Request failed
java.lang.NullPointerException
(no message, no stack trace from here on)
Step throughHow a full trace turns into a bare NPE
  1. Code dereferences nullinterpreted or early compiled code
  2. JVM creates a new NPEfull stack trace and helpful message
  3. Same throw happens many timesthe code is hot and gets recompiled by C2
  4. JIT sees a hot implicit throwOmitStackTraceInFastThrow is on
  5. Compiled code throws a shared NPEno stack trace, no message
  6. Logs show bare NullPointerExceptionuntil a restart or deoptimization

6 steps in order. Play them, or step through them one at a time.

3Finding the Original Stack Trace

The first thing to do is look back in time. Before the code became hot, every NPE was a fresh object with a full trace and, on Java 15 and later, a helpful message. So we search the logs from shortly after the last deployment or restart for the first NullPointerException entries. They show exactly where the null was dereferenced and what was null. Restarting a single instance briefly also brings full traces back until the code is hot again, though it is better not to rely on that.

Notice that the bare entries all come from the same place, but we cannot prove that from the bare entries themselves. If several different bugs throw NPEs, each hot site switches to the shared instance independently, so the early full traces may show more than one location, and each needs looking at.

4Turning the Optimization Off

If we need full traces on every throw, for example while investigating, we can disable the optimization.

Shell
java -XX:-OmitStackTraceInFastThrow -jar app.jar
# Every implicit exception now gets a fresh object with a stack trace and message.
# Check the running value:
jcmd 1 VM.flags -all | grep OmitStackTraceInFastThrow
# bool OmitStackTraceInFastThrow = false

The cost is that each of those exceptions builds a full stack trace again. If the service is throwing thousands of NPEs per second, that cost can be noticeable, which tells us something important: an exception thrown that often is not a rare event but a bug on a hot path. Many teams leave the flag off permanently, because the performance gain only matters in services that throw implicit exceptions constantly, which is itself a problem to fix.

5Fixing the Real Problem

The optimization only kicks in because the same NPE is happening very frequently. That is the bug. In the example above, the full trace tells us that Customer.getEmail() returned null and EmailValidator called length() on it. So we decide whether a customer may lack an email. If it may, the validator must handle absence explicitly; if it may not, the data must be validated where it enters the system, for example with Bean Validation on the request or a NOT NULL constraint in the database, and Objects.requireNonNull in the constructor so the failure happens early with a clear message.

We also count NullPointerExceptions in metrics, for example through the shared error handler with the exception type as a tag. A rising count is visible on a dashboard long before anyone reads the logs, and it confirms when the fix has worked.

6Follow-up questions

Does the optimization apply to exceptions thrown explicitly with throw new?

No. It applies to implicit exceptions created by the JVM when an instruction fails. An exception created with throw new always runs its constructor and fills in its stack trace, unless its class chooses otherwise.

Why does the message disappear as well as the stack trace?

Because the preallocated object is shared across every site and thread, it cannot carry a message specific to one site, so it has none. Helpful NullPointerException messages are computed for fresh NPE objects, so they are lost too.

Can the full traces come back without a restart?

Sometimes. If the JIT deoptimizes the method, for example because a class is loaded that invalidates an assumption, it runs in the interpreter for a while and fresh exceptions appear again until it is recompiled. This is not something to rely on; turning the flag off or finding early log entries is the dependable approach.

7Conclusion

In this answer, we saw that bare NullPointerExceptions after a service has run for a while come from HotSpot's OmitStackTraceInFastThrow, which makes hot, compiled code throw a preallocated implicit exception without stack trace or message. It is on by default and -XX:-OmitStackTraceInFastThrow turns it off. The first occurrences in earlier logs still have full traces, and because the optimization only triggers for exceptions thrown very often, the real fix is to find and remove the cause of the frequent NPE.

What to say in the interviewAbout 90 seconds aloud

Open with

This is the JIT optimization called OmitStackTraceInFastThrow. For implicit exceptions, the ones the JVM creates itself, like NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException, ArrayStoreException and ClassCastException, when compiled code throws the same exception at the same place very often, C2 recompiles it to throw a preallocated, shared exception object.

Then, if they want more

That object has no stack trace and no message, so it is cheap but tells us nothing. It is on by default, and it only kicks in once the code is hot, which is why the early occurrences after a restart had full traces and later ones are bare.

So the first thing I do is search the logs from shortly after the last deploy or restart for the first NPEs, which have the full trace and, since Java 15, the helpful NPE message saying exactly what was null.

If I need full traces all the time, I run with -XX:-OmitStackTraceInFastThrow, and many teams leave it off because the saving only matters when you are throwing constantly. But the bigger point is that an NPE thrown often enough to trigger this is a real bug on a hot path.

So I fix the cause, for example by validating input at the boundary or handling the absent value explicitly, and I track NPE counts in metrics to confirm it is gone.

About this answer

Last updated
12 November 2026
Versions it names
Java 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Production logs show NullPointerException with no stack trace and no message after the...": https://scrivio-interview-library.netlify.app/java/exceptions/49914a0cef/