Useful message about NullPointerException

pike pike630 at hotmail.com
Tue Jan 27 13:00:56 UTC 2015


Chris Newland wrote
> Hi,
> 
> If the contract for a method allows a null return you should really handle
> it defensively. Null checks are cheap in terms of performance.
> 
> If you *have* to chain method calls like that then you could drop each
> call onto a new source line and the stack trace will show you which call
> was on the null object:
> 
> public class NullTest
> {
>     class B {
>         private String c = null;
>         public String getC() { return c; }
>     }
> 
>     class A {
>         private B b = null;
>         public B getB() { return b; }
>     }
> 
>     public NullTest() {
>         new A()
>         .getB()
>         .getC() // NPE here indicates getB() returned null
>         .length();
>     }
> 
>     public static void main(String[] args) {
>         new NullTest();
>     }
> }
> 
> Regards,
> 
> Chris
> @chriswhocodes
> 
> On Tue, January 27, 2015 11:47, pike wrote:
>> Bernd Eckenfels-4 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.
>>>
>>> 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
>>>
>>
>> This is acually not helpful in some situations. For example, as what
>> kedar has mentioned, if there is a call "a.getB().getC()" and a NPE  is
>> thrown. There might be two situations:
>> 1. a is null;
>> 2. a.getB() is null
>>
>>
>> So it would be better if there can be one friendly message which
>> indicates what is null.
>>
>>
>>
>> --
>> View this message in context:
>> http://openjdk.5641.n7.nabble.com/Useful-message-about-NullPointerExcepti
>> on-tp213240p213842.html Sent from the OpenJDK Core Libraries mailing list
>> archive at Nabble.com.
>>

You are advising we should stick to the best coding style. But many times,
not everyone can stick to this. If you are handling something written by
others and you have no access to the source, you would appreciate if NPE can
provide more friendly message like "The object reference a.getB() is
null"... 



-----
Programmer
--
View this message in context: http://openjdk.5641.n7.nabble.com/Useful-message-about-NullPointerException-tp213240p213855.html
Sent from the OpenJDK Core Libraries mailing list archive at Nabble.com.



More information about the core-libs-dev mailing list