Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Throwing Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Throwing Null Pointer Exception

    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....:/


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Throwing Null Pointer Exception

    Quote Originally Posted by SaurabhUpadhyay View Post
    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.)
    Last edited by 2by4; December 11th, 2011 at 05:51 AM.

Similar Threads

  1. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  2. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM