tags:

views:

148

answers:

5

Would it be useful to include the class name and variable name in any NullPointerException message? I know that it might not always be possible because of changes made by a JIT but is seems like the info should be available often (class members, etc).

From: http://jamesjava.blogspot.com/2005/04/what-was-null.html

+1  A: 

Providing as much information as possible during errors is a good thing... Right?

Helps out tracking the bugs.

Edit: (Yes)

Statement
A: 

Are you throwing NullPointerException? I think you should probably do null validation in the code. I would also consider using open source logging tools like Log4J.

Soumitra
+2  A: 

That depends. If you get the stack trace, it is clear what class threw the exception. This usually leads to making sure your environment will give you stack traces when there are unhanded exceptions.

Milhous
The stack trace normally gives you only the line number, so if more than one objects is referenced on that line you still don't know which caused the exception
Thomas
A: 

Yes, that would be useful. Especially if you have a mechanism where the error message (exception.getMessage()) is displayed on-screen but the actual stacktrace gets hidden away in log files which you can't access immediately.

Roy Tang
+1  A: 

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.

Aleksandar Dimitrov
In production with hard to reproduce problems that really isn't a good option.
James A. N. Stauffer
Well, you're already getting a line number. You actually know what variable is null (unless you were stupid enough to produce code that dereferences objects twice or thrice or even more often on a single line).
Aleksandar Dimitrov