# Thread: Checking if a reference variable is empty.

1. Member
Join Date
Aug 2011
Posts
5
Rep Power
0

## 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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## Re: Checking if a reference variable is empty.

if(whatever==null)

3. Hostage
Join Date
Apr 2013
Location
China
Posts
128
Rep Power
0

## 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 04:38 PM.

4. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## 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. Hostage
Join Date
Apr 2013
Location
China
Posts
128
Rep Power
0

## Re: Checking if a reference variable is empty.

Originally Posted by Ronin
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!"

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. ## 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. Hostage
Join Date
Apr 2013
Location
China
Posts
128
Rep Power
0

## Re: Checking if a reference variable is empty.

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
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. ## 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. Hostage
Join Date
Apr 2013
Location
China
Posts
128
Rep Power
0

## 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 10:03 AM.

10. Moderator
Join Date
Apr 2009
Posts
13,368
Rep Power
24

## Re: Checking if a reference variable is empty.

Because enclosing it all in a try/catch is not good coding practice?

11. Hostage
Join Date
Apr 2013
Location
China
Posts
128
Rep Power
0

## Re: Checking if a reference variable is empty.

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.

12. Moderator
Join Date
Apr 2009
Posts
13,368
Rep Power
24

## 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...

#### Posting Permissions

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