No, I don't think that this would at all be "useful".
Instead, you should watch out not to throw NPEs in the first place. Checking your values before you use them (null
validation) and the likes. This should happen before you call a library method and after you get back a result (iff the API specifies that the method may return null
. Of course, sometimes it will do so anyways, but then that's a bug).
If you think that NPEs should carry this information for debugging, think again. That's what debuggers are good for. An exception is simply there to inform you that something has gone wrong. Remember that unchecked exceptions occur at runtime - and have to be generated there. In order for the Exception to know which variable contained null
, the byte code would have to know about variable names. You don't want to bloat your byte code with stuff like this. The class name is contained in every logging output I recieve from my programs. That's what logging is good for.
Java already eases the debugging process a lot by giving you the line number and full stack trace. C programs fail with Segmentation fault
. You have to use strace
or a debugger in order to even get so much information.
Note that javac
does include a compile time option to include source file information at compile time, but this is intended to be used by a debugger, not the random Exceptions that get thrown. Quoting Sun's javac
man page:
-g Generate all debugging information, including local variables.
By default, only line number and source file information is
generated.
-g:none
Do not generate any debugging information.
-g:{keyword list}
Generate only some kinds of debugging information, specified
by a comma separated list of keywords. Valid keywords are:
source
Source file debugging information
lines
Line number debugging information
vars
Local variable debugging information
Long story short: use a debugger.