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 9 of 9

Thread: NullPointerException.CLARIFICATION

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default NullPointerException.CLARIFICATION

    there are no pointers in java,

    but pointer means variable, is it right?

    so when i encounter these kind of error(NullPointerExcpeion) it means that .. the variable that im reffering to, does not contain anything.(Null or no value) ,do i make any sense about this matter?
    am i right?
    Last edited by chronoz13; August 27th, 2009 at 06:35 AM.

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException.CLARIFICATION

    The way Java's object variables work is by using pointers of sorts. The only thing is you can't modify the value directly, you have to reference it to some existing object or create a new one and reference to it.

    Null Pointer Exception just means that your variable is not pointing to an object. Since you can't manage memory primitively with pure Java, you can't delete an object when something is still referencing it, so it's impossible to have an "empty" object.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: NullPointerException.CLARIFICATION

    ahm helloworld

    can you give some code examples of that, im a bit confused... please......


    tnx tnx tnx.....

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException.CLARIFICATION

    You were right in your first post, when a variable is assigned with null it is actually pointing to null/nothing which means that if you try to invoke a method on that null reference will give you a NullPointerException.

    Taken from NullPointerException (Java Platform SE 6)

    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.
    Taking the length of null as if it were an array.
    Accessing or modifying the slots of null as if it were an array.
    Throwing null as if it were a Throwable value.
    // Json

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: NullPointerException.CLARIFICATION

    tnx json i tried to output an error like that with two different simple program

    here it is

    first code

    String word = null;
     
    System.out.print(word);

    it only prints the word null

    BUT-

    second code

      String word = null;
           StringBuilder builder = new StringBuilder(word);
     
            System.out.println(builder);

    i've noticed the difference between the two

    can i consider the second code as "Referencing"
    then if it is..

    im cleared out

    tnx Json for that information

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException.CLARIFICATION

    Try this one.

    String word = null;
     
    String upperCaseWord = word.toUpperCase();

    // Json

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Smile Re: NullPointerException.CLARIFICATION

    same output sir

    is that what they call "Referencing" ?

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: NullPointerException.CLARIFICATION

    Id say its because of this.

    Accessing or modifying the field of a null object.
    // Json

  9. The Following User Says Thank You to Json For This Useful Post:

    chronoz13 (August 28th, 2009)

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NullPointerException.CLARIFICATION

    String word = null;

    System.out.print(word);
    The reason it prints out "null" is somewhere inside the print method there's a line that looks like this:
    if (txt == null)
    {
        //printout "null"
    }

    I believe the print method does a similar thing when you try 1/0 (except it'll print out infinity, or NA)

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  3. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  4. Replies: 3
    Last Post: February 26th, 2009, 03:04 AM
  5. NullPointerException in Java
    By jazz2k8 in forum Exceptions
    Replies: 9
    Last Post: June 1st, 2008, 05:59 PM