Jump to content
  • Advertisement
Sign in to follow this  
Zaxx

[java] Checking if element in array of objects is null

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Right now I'm working on a program where I'm looping through an array of objects. For each element I want to check if any of the adjacent elements are null. So, an example check would be: if (ary[row - 1][col - 1].equals(null)) ary[row][col].doStuff(); The problem is that I get a null pointer exception inside the boolean check, which seems strange since I'm checking IF an element is null. I've also tried (ary[row - 1][col - 1] == null), but that doesn't work either. I'll probably have to look at the row and col variables directly to see if they're off the array, but does anyone here have a better solution?

Share this post


Link to post
Share on other sites
Advertisement

if (ary[row - 1][col - 1] == null)
ary[row][col].doStuff();

You cannot call any function on a null object, not even equals!

Share this post


Link to post
Share on other sites
So when you use .equals(), you obviously get a NullPointerException because the object is null. BUT, when you use the == null method, I guarantee you're no longer getting a NullPointerException, but an ArrayIndexOutOfBoundsException. *Obviously* you will have to make sure that row and col are within bounds. This is A Good Thing. If you tried to do something like this with C++, hello undefined behavior! Java is saving you.

Share this post


Link to post
Share on other sites
There are two ways of detecting if an object is null.

The first and most common is the use of the "null" key word in an if statement.

before you call any methods of an object that could be null you should surround those calls with an if statement the like the one that follows

if( NullObj == null ) {
//some calls...
NullObj.objmethod1();
//some other processing code...
...
NullObj.objmethod2();
...
}

The second method and not so widely used is to simply trap the null pointer exception although the later is prefered trapping the null pointer exception might come in handy.

try {
//some calls...
NullObj.objmethod1();
//some other processing code...
...
NullObj.objmethod2();
...
}catch (NullPointerException npe ) {
//do case specific handling for instance
nullobj = new Object();
}

That should be helpful.

As far as the == null being a problem with your two dimensional array check to make sure that the two dimensional array isn't primitives and is defined as Objects. Also check to make sure that when your defining your Array that you just haven't made it equal to null such as the line below.

Integer marray[][] = null;

The above will give you a null pointer exception whenever you try to access any specific member of the array such as:

Integer someInt = marray[1][3];

When you set the array to null it means Java should go ahead and make the variable and set it equal to null instead of being equal to an array.
You should make the initialization as follows.

Integer marray[][] = new Integer[3][3];

Let me know if that solves the problem.

Share this post


Link to post
Share on other sites
Optus was right. I was actually getting the ArrayIndexOutOfBoundsException.

I'm just going to check the row and col values now. Thanks.

Share this post


Link to post
Share on other sites
And just a reminder that Java arrays are objects, and multi-dimensional arrays are thus not necessarily rectangular, or even initialized properly.

if (ary[row - 1][col - 1] == null)


This CAN give a NullPointerException - if ary[row-1] is null, or if ary is null. :)

Anyway, in general, prefer explicit checks to the .equals() method whenever possible (but do use .equals() if it is required for correctness, i.e. object equality means something other than object identity for that class, AND you are interested in the former).

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaxx
Well, as I said, I get errors when I use "== null" too.


I'm sorry for not reading your whole post.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!