Originally Posted by
SaurabhUpadhyay
I have been Google database handling packages. Now I am getting a Null Pointer Exception in the following line :
birthDate = "" + entry.getBirthday().getValue();
birthday is a variable declared with " " at beginning. getBirthday() and getValue() are Google Database functions....
Moreover if I am using :
if(entry.getBirthday().getValue()!=null)
birthDate = "" + entry.getBirthday().getValue();
the compiler throws Null Pointer Exception on the if statement....:/
1. It is throwing an exception because it CANNOT evaluate the expression, not because the whole expression evaluates to
null: there is nothing wrong, per se, with an expression evaluating to
null or even concatenating a
null with a
String :-)
2. It cannot evaluate the expression because an object reference WITHIN the expression entry.getBirthday().getValue() evaluates to
null AND is being used to reference a scoped member (field or method) of that object.
Examine the expression and review the steps taken at runtime to evaluate it.
Hint: The runtime starts by accessing an object through a reference. Then it calls a method of that object which returns a reference to another object...
Break the expression up to check if either of these objects is
null.
(The runtime finally calls a method on the second object which returns your final value. Even if this final value is an object, there would be no problem with it being
null, as it is not being used to access a member. So your
if statement is checking the wrong value.)