vote up 0 vote down star

I have a partially nfilled array of objects, and when I iterate through them I tried to check to see whether the selected object is null before I do other stuff with it. However, even the act of checking if it is null seem to through a NullPointerException. array.length will include all null elements as well. How do you go about checking for null elements in an array? For example in the following code will throw an NPE for me.

Object[][] someArray = new Object[5][];
for (int i=0; i<=someArray.length-1; i++)
{
    if (someArray[i]!=null) { //do something
    } 
}
flag
1  
your code doesn't give me a NPR. You might also want to use "i<someArray.length" instead of "i<=someArray.length-1" – d0k Jan 8 '09 at 19:11

6 Answers

vote up 2 vote down check

You have more going on than you said. I ran the following expanded test from your example:

public class test {

    public static void main(String[] args) {
        Object[][] someArray = new Object[5][];
        someArray[0] = new Object[10];
        someArray[1] = null;
        someArray[2] = new Object[1];
        someArray[3] = null;
        someArray[4] = new Object[5];

        for (int i=0; i<=someArray.length-1; i++) {
            if (someArray[i] != null) {
                System.out.println("not null");
            } else {
                System.out.println("null");
            }
        }
    }
}

and got the expected output:

$ /cygdrive/c/Program\ Files/Java/jdk1.6.0_03/bin/java -cp . test
not null
null
not null
null
not null

Are you possibly trying to check the lengths of someArray[index]?

link|flag
1  
No NPE for me either. – mmyers Jan 8 '09 at 19:09
Thanks! I will look into it more. – ckeven Jan 8 '09 at 19:17
vote up 2 vote down

It does not.

See below. The program you posted runs as supposed.

C:\oreyes\samples\java\arrays>type ArrayNullTest.java
public class ArrayNullTest {
    public static void main( String [] args ) {
        Object[][] someArray = new Object[5][];
            for (int i=0; i<=someArray.length-1; i++) {
                 if (someArray[i]!=null ) {
                     System.out.println("It wasn't null");
                 } else {
                     System.out.printf("Element at %d was null \n", i );
                 }
             }
     }
}


C:\oreyes\samples\java\arrays>javac ArrayNullTest.java

C:\oreyes\samples\java\arrays>java ArrayNullTest
Element at 0 was null
Element at 1 was null
Element at 2 was null
Element at 3 was null
Element at 4 was null

C:\oreyes\samples\java\arrays>
link|flag
vote up 1 vote down

The given code works for me. Notice that someArray[i] is always null since you have not initialized the second dimension of the array.

link|flag
vote up 1 vote down

Well, first of all that code doesn't compile.

After removing the extra semicolon after i++, it compiles and runs fine for me.

link|flag
I already removed it. :) – mmyers Jan 8 '09 at 19:12
vote up 1 vote down

The example code does not throw an NPE. (there also should not be a ';' behind the i++)

link|flag
vote up 0 vote down

Fighting whether the code is compiling or not I would say create a array of sixe 5 add 2 values and print them , you will get the two values and others are null. The question is although the size is 5 but there are 2 objects in the array . How to find how many objects are present in the array

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.