How to check if array element is null to avoid NullPointerException in Java

后端 未结 9 1044
情深已故
情深已故 2020-12-16 13:08

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

相关标签:
9条回答
  • 2020-12-16 13:16

    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

    0 讨论(0)
  • 2020-12-16 13:16

    You can do it on one line of code (without array declaration):

    object[] someArray = new object[] 
    {
        "aaaa",
        3,
        null
    };
    bool containsSomeNull = someArray.Any(x => x == null);
    
    0 讨论(0)
  • 2020-12-16 13:19

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

    0 讨论(0)
  • 2020-12-16 13:23
    String labels[] = { "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" }; 
    
    if(Arrays.toString(labels).indexOf("null") > -1)  {
        System.out.println("Array Element Must not be null");
                         (or)
        throw new Exception("Array Element Must not be null");
    }        
    ------------------------------------------------------------------------------------------         
    
    For two Dimensional array
    
    String labels2[][] = {{ "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" },{ "MH", "FG", "AP", "KL", "CH", "MP", "GJ", "OR" };    
    
    if(Arrays.deepToString(labels2).indexOf("null") > -1)  {
        System.out.println("Array Element Must not be null");
                     (or)
        throw new Exception("Array Element Must not be null");
    }    
    ------------------------------------------------------------------------------------------
    
    same for Object Array    
    
    String ObjectArray[][] = {{ "MH", null, "AP", "KL", "CH", "MP", "GJ", "OR" },{ "MH", "FG", "AP", "KL", "CH", "MP", "GJ", "OR" };    
    
    if(Arrays.deepToString(ObjectArray).indexOf("null") > -1)  {
        System.out.println("Array Element Must not be null");
                  (or)
        throw new Exception("Array Element Must not be null");
      }
    

    If you want to find a particular null element, you should use for loop as above said .

    0 讨论(0)
  • 2020-12-16 13:28

    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]?

    0 讨论(0)
  • 2020-12-16 13:33

    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>
    
    0 讨论(0)
提交回复
热议问题