# Checking if a reference variable is empty.

• 04-06-2013, 02:55 AM
BigBlueBuffalo
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!

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;         }     } }```
• 04-06-2013, 03:05 AM
pbrockway2
Re: Checking if a reference variable is empty.
if(whatever==null)
• 04-07-2013, 04:26 PM
NotTooOld
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.
• 04-07-2013, 11:20 PM
Ronin
Re: Checking if a reference variable is empty.
You would be better using the following:
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.
• 04-08-2013, 04:06 AM
NotTooOld
Re: Checking if a reference variable is empty.
Quote:

Originally Posted by Ronin
You would be better using the following:
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!"

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;                 }```
• 04-09-2013, 04:38 AM
Junky
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.
• 04-09-2013, 05:39 AM
NotTooOld
Re: Checking if a reference variable is empty.
Quote:

Originally Posted by Junky
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
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.
• 04-09-2013, 06:12 AM
Junky
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.

Code:

```for(...) {     if(...) {         ...     } else {         ...     }     y++; }```
Do you really find that hard to understand?
• 04-09-2013, 06:58 AM
NotTooOld
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.
• 04-09-2013, 09:37 AM
Tolls
Re: Checking if a reference variable is empty.
Because enclosing it all in a try/catch is not good coding practice?
• 04-09-2013, 10:00 AM
NotTooOld
Re: Checking if a reference variable is empty.
Quote:

Originally Posted by Tolls
Because enclosing it all in a try/catch is not good coding practice?

I won't argue with you, you are far too helpful.
• 04-09-2013, 10:14 AM
Tolls
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...