Useful message about NullPointerException

pike pike630 at
Tue Jan 27 11:51:51 UTC 2015

kedar mhaswade wrote
> On Mon, Jan 26, 2015 at 11:20 AM, Bernd Eckenfels <

> ecki@

> >
> wrote:
>> Am Wed, 21 Jan 2015 05:45:08 -0700 (MST)
>> schrieb pike <

> pike630@

> >:
>> > We frequently see NullPointerException in our logs. It's really a big
>> > headache when we see a NullPointerException and it is encapsulated in
>> > another exception as we don't know which object is null and it is
>> > throwing an Exception. Is there any way we can get to know the object
>> > type or the object variable name where the object is null and it is
>> > throwing a NullPointerException? i.e, instead of just saying there is
>> > a NullPointerException, can we add some friendly message?
>> Note that if you keep the stack information in an exception it points
>> (most of the time) exactly to the location where the null access
>> happens.
> ​Maybe the objects in question are not talking only to their immediate
> friends <>?
> This happens when we tend to do something like *a.getB().getC()* and we
> can't tell by looking at the NPE stack trace (which works on lines of
> source code) if *a* was null or what *a.getB()* returned was null.
> I can imagine it is rather hard for the VM to add more informations.
>> Your best bet is to avoid the NPEs and log the exceptions properly.
>> Gruss
>> Bernd

Hmm, based on the JVM specification, there is Descriptor structure which can
be used to describe the name and type of a field or method in a class. I
think, the JVM can use this info to display the type of the object which is
null when NPE is thrown.

Furthermore, I think the constant pool also includes all the information
about the fields in a class. They can be used as well.

View this message in context:
Sent from the OpenJDK Core Libraries mailing list archive at

More information about the core-libs-dev mailing list