When a multi-step single line statement results in a NullPointerException (NPE), the stack trace indicates the line number where the NPE was thrown. Is it possible for the JVM to provide any additional information at all in the NPE message to narrow down which statement on the line actually resulted in the NPE?
E.g.
myObject.getSomeField().doSomething();
Either myObject
or myObject.getSomeField()
could be null, and both would result in the same stack trace. Is it technically possible for the JVM to provide additional information here to help reduce the ambiguity?
There are many ways to avoid this in code, ranging from simply spreading the steps across multiple lines:
myObject
.getSomeField()
.doSomething();
to checking for null and handling appropriately at each step:
if (myObject != null)
{
SomeField myField = myObject.getSomeField();
if (myField != null)
{
...
as well as solutions using the ternary operator or other shortcuts.
However, for this question I am strictly concerned with what the JVM can do based on the information it already has available to it. Does the JVM have additional information that it could include in the NPE message to mitigate any uncertainty?