Results 1 to 3 of 3

Thread: Null Object

  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Null Object

    Hello,

    Im trying to make my program print out "It is null" when a rectangle object(r1) hasn't been created,
    but I am getting the error: java.lang.NullPointerException instead of "It is null"

    Java Code:
    if( r1 != null  )
        System.out.println("It is null");
    Could someone please tell me what i've done wrong?

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default Re: Null Object

    Not sure I understand. If you do if (r1 != null), then you must have created an r1 object with r1 = new..." somewhere. Otherwise it should say that r1 can't be resolved and your program won't even compile. Or am I completely misunderstanding your question?

  3. #3
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Null Object

    Hello,
    I'm not sure about your error, but it appears that your code is written so that if r1 is NOT NULL, then print the statement. Are you sure you want your code to act this way?

Similar Threads

  1. Replies: 5
    Last Post: 09-17-2011, 01:35 AM
  2. Size of serialized object with null values
    By jamesd128 in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 06:43 PM
  3. Null Pointer Exception for formatter object
    By andre1011 in forum Networking
    Replies: 0
    Last Post: 03-28-2009, 10:51 PM
  4. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM
  5. what happen if an object set to null
    By SaYuNaRa in forum New To Java
    Replies: 5
    Last Post: 04-17-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •