Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default Checking if a reference variable is empty.

    How could I check if a reference variable is empty? I'm doing this question from a book and I want to print out what number they are pointing to. The thing is, some of them point to nothing, so when I run a while loop I get an error saying java.lang.NullPointerException. Any help would be great!

    Java Code:
    package heapquiz;
    
    public class HeapQuiz {
    
        int id = 0;
        
        public static void main(String[] args) {
            int x = 0;
            HeapQuiz [] hq = new HeapQuiz[5];
            
            while (x < 3){
                hq[x] = new HeapQuiz();
                hq[x].id = x;
                x += 1;
            }
            
            hq[3] = hq[1];
            hq[4] = hq[1];
            hq[3] = null;
            hq[4] = hq[0];
            hq[0] = hq[3];
            hq[3] = hq[2];
            hq[2] = hq[0];
            
            int y = 0;
            while (y < 5){
                System.out.println("hq[" + y + "] == " + hq[y].id);
                y += 1;
            }
        }
    
    }

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Checking if a reference variable is empty.

    if(whatever==null)

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Checking if a reference variable is empty.

    If I understand this, you are getting the nullPointerException because you only put 4 values in hq. Look what happens with hq[3]. In the first of the 7 assignments, you assign hq[3] =hq[1] ok so far, but then you assign it the value null. you then set hq[0] = hq[3], so now hq[0] is equal to null. You get a NullPointerException as soon as you try to print. Correct?
    If I have your problem correct, this should fix it:

    int y = 0;
    while (y < 5){
    if (!hq[y].id == null) { ////// This should skip over the null value
    System.out.println("hq[" + y + "] == " + hq[y].id); y += 1;
    }
    y += 1;
    }

    Don't blindly trust me. I'm a noob at this.
    Last edited by NotTooOld; 04-07-2013 at 05:38 PM.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Checking if a reference variable is empty.

    You would be better using the following:
    Java Code:
    if(!hq[y]==null)
    This validate the specific instance within the array as opposed to the attribute.

    Additonally, I'm assuming 'y' is the number of elements within 'hq'. If so, you could use hq.length instead. The benefit of this being you are defining the number of iterations based on the length of the array. Else, if the size of the array changes for any reason you would have to make two changes to the code as opposed to just one.
    It sounds trivial now but when these values are burried under n^1000 lines of code it becomes diffcult to find them.

    Regards.

  5. #5
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Checking if a reference variable is empty.

    Quote Originally Posted by Ronin View Post
    You would be better using the following:
    Java Code:
    if(!hq[y]==null)
    This validate the specific instance within the array as opposed to the attribute.

    Additonally, I'm assuming 'y' is the number of elements within 'hq'. If so, you could use hq.length instead. The benefit of this being you are defining the number of iterations based on the length of the array. Else, if the size of the array changes for any reason you would have to make two changes to the code as opposed to just one.
    It sounds trivial now but when these values are burried under n^1000 lines of code it becomes diffcult to find them.

    Regards.
    I concur. My call to a field in a null object would also throw the NullPointerException. "Told you I was new!"

    How about this?
    Java Code:
    int y = 0;         
             while (y < hq.length) {
            	 if (hq[y] == null){
            		 System.out.println("hq[" + y + "] == null");
            		 y += 1;
            	 }
            	 else {
            		 System.out.println("hq[" + y + "] == " + hq[y].id);
            		 y += 1;
            	 }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Checking if a reference variable is empty.

    Why increment y in both branches instead of once after the if statement? Even better use a for each loop.

  7. #7
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Checking if a reference variable is empty.

    Quote Originally Posted by Junky View Post
    Why increment y in both branches instead of once after the if statement? Even better use a for each loop.
    Because this is in the "New to Java" forum, and we don't know how familiar this poster is with the language. If statements are probably the easiest to understand. Adding
    Java Code:
     
                y++;
    //           or
               y += 1
    outside of the if statements makes it harder to understand. Baby steps. We could show him how to write a method to call and handle it too, but should we? I tried to change as little as possible from the code the poster understands.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Checking if a reference variable is empty.

    I totally disagree. Seeing duplicate code is more confusing to me. As you point out this is "new to java" therefore we should be encouraging learners to use good coding practices and eliminate duplicate code.

    Java Code:
    for(...) {
        if(...) {
            ...
        } else {
            ...
        }
        y++;
    }
    Do you really find that hard to understand?

  9. #9
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Checking if a reference variable is empty.

    I do not find it more difficult to understand, but the fact is you are breaking it into three parts, and I think considering two parts easier than three. {do this, or: this} OR {do this, and then this, or: do that, and then this} We can disagree on which is easiest to understand.

    The poster wanted to know how to check if a variable is empty, and said, "The thing is, some of them point to nothing, so when I run a while loop I get an error saying java.lang.NullPointerException." With that in mind I quickly showed him "one way" to handle his Null exception. As far as "good coding practice" why not advise enclosing it all in a try/catch... moving on.
    Last edited by NotTooOld; 04-09-2013 at 11:03 AM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Checking if a reference variable is empty.

    Because enclosing it all in a try/catch is not good coding practice?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: Checking if a reference variable is empty.

    Quote Originally Posted by Tolls View Post
    Because enclosing it all in a try/catch is not good coding practice?
    I won't argue with you, you are far too helpful.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default Re: Checking if a reference variable is empty.

    :)

    To be fair, I know what you're getting at (at least I hope so) in that it is good to catch exceptions before they reach the user so they can be turned into something meaningful. In a situation like this it's not really applicable, but it is something to bear in mind for larger systems.

    A bit like whether your if/else makes more sense to someone than Junky's...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. reference variable or an attribute value
    By agater in forum New To Java
    Replies: 1
    Last Post: 04-23-2012, 12:49 PM
  2. Checking to see if a JTextField is empty.
    By Dark in forum New To Java
    Replies: 16
    Last Post: 06-24-2011, 02:36 PM
  3. Reference variable
    By lala in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 08:27 PM
  4. Checking if a variable is set in jsp
    By saurabh1989 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 02-02-2011, 03:14 PM
  5. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 09:17 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
  •