2009-01-08 50 views
19

我有一個部分未充滿的對象數組,當我遍歷它們時,我試着檢查選定的對象是否爲null,然後再執行其他操作。但是,即使是通過NullPointerException來檢查它是否爲nullarray.length也將包括所有的null元素。你如何去檢查數組中的null元素?例如在下面的代碼中會爲我提供一個NPE。如何檢查數組元素是否爲null,以避免Java中的NullPointerException

Object[][] someArray = new Object[5][]; 
for (int i=0; i<=someArray.length-1; i++) { 
    if (someArray[i]!=null) { //do something 
    } 
} 
+1

您的代碼不給我一個NPR。您可能還想使用「i d0k 2009-01-08 19:11:00

+0

如果您檢查`!someArray.equals(null)`,它會拋出NPE。 – bancer 2010-11-08 22:45:04

回答

22

你比你說的還多。我跑從比如下面的擴展測試:

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"); 
      } 
     } 
    } 
} 

,並得到了預期的輸出:

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

你可能想檢查的someArray [index]的長度?

+1

我也沒有NPE。 – 2009-01-08 19:09:18

1

給定的代碼適用於我。請注意,someArray [i]始終爲空,因爲您尚未初始化數組的第二維。

1

那麼,首先,代碼不會編譯。

在i ++之後刪除多餘的分號後,它編譯並運行正常。

6

它沒有。

見下文。您發佈的程序按假定運行。

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> 
1

示例代碼不會拋出NPE。 (也不應該有一個';'後面的i ++)

0

戰鬥是否編譯代碼我會說創建一個數組6e 5添加2值並打印它們,你會得到兩個值和其他爲空。問題是儘管大小是5,但數組中有2個對象。如何找到陣列中有多少物體

2
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"); 
    } 

如果您想查找某個特定的null元素,則應如上所述使用for循環。

0
public static void main(String s[]) 
{ 
    int firstArray[] = {2, 14, 6, 82, 22}; 
    int secondArray[] = {3, 16, 12, 14, 48, 96}; 
    int number = getCommonMinimumNumber(firstArray, secondArray); 
    System.out.println("The number is " + number); 

} 
public static int getCommonMinimumNumber(int firstSeries[], int secondSeries[]) 
{ 
    Integer result =0; 
    if (firstSeries.length !=0 && secondSeries.length !=0) 
    { 
     series(firstSeries); 
     series(secondSeries); 
     one : for (int i = 0 ; i < firstSeries.length; i++) 
     { 
      for (int j = 0; j < secondSeries.length; j++) 
       if (firstSeries[i] ==secondSeries[j]) 
       { 
        result =firstSeries[i]; 
        break one; 
       } 
       else 
        result = -999; 
     } 
    } 
    else if (firstSeries == Null || secondSeries == null) 
     result =-999; 

    else 
     result = -999; 

    return result; 
} 

public static int[] series(int number[]) 
{ 

    int temp; 
    boolean fixed = false; 
    while(fixed == false) 
    { 
     fixed = true; 
     for (int i =0 ; i < number.length-1; i++) 
     { 
      if (number[i] > number[i+1]) 
      { 
       temp = number[i+1]; 
       number[i+1] = number[i]; 
       number[i] = temp; 
       fixed = false; 
      } 
     } 
    } 
    /*for (int i =0 ;i< number.length;i++) 
    System.out.print(number[i]+",");*/ 
    return number; 

} 
0

你可以做到這一點的一行代碼(沒有數組聲明):

object[] someArray = new object[] 
{ 
    "aaaa", 
    3, 
    null 
}; 
bool containsSomeNull = someArray.Any(x => x == null); 
相關問題