In response to Can Java's NullPointerException be changed to report on which variable was null? thorbjorn-ravn-andersen says:
No, the debug information in the class file does not contain enough information to allow this.
My question is, why not?
In response to Can Java's NullPointerException be changed to report on which variable was null? thorbjorn-ravn-andersen says:
No, the debug information in the class file does not contain enough information to allow this.
My question is, why not?
The actual exception points to a particular bytecode instruction, but a language statement may expand into dozens of instructions with a half-dozen different pointers referenced. Additionally, the pointers may have been held in temporary storage (bytecode stack) for the duration of several instructions, so the association with a specific variable name is likely lost.
A fancy debugger could make a fair guess at the variable that was "at fault", but, generally speaking, being pointed to the failing statement should be sufficient (though bearing in mind that the failure may be due to a prior statement whose action was deferred).
It may not have been a variable at all: it may have been a temporary result, e.g. the result of a method call. By the time the NPE occurs it is just an anonymous stack slot.
Because line number in the code is sufficient enough to trace it.