Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a class file not contain sufficient information to provide the name of the pointer that was null in a NPE?

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?

like image 981
Ken Taylor Avatar asked Jan 02 '13 21:01

Ken Taylor


People also ask

Why do we get NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do you fix a NullPointerException in Java?

NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

Why does NullPointerException occur in selenium Webdriver?

NullPointerException is thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.

What is a null pointer error?

June 01, 2022. CWE-476 Null Pointer Dereference is a programming error that can occur when a program attempts to deference a null pointer. This can happen when the programmer mistakenly assumes that a pointer pointing to NULL is actually pointing to a valid object.


2 Answers

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.

like image 84
user207421 Avatar answered Oct 06 '22 02:10

user207421


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).

like image 34
Hot Licks Avatar answered Oct 06 '22 02:10

Hot Licks