JavaでNullPointerExceptionを回避するために配列の要素がNULLであるかどうかを確認する方法

オブジェクトの部分的な nfilled 配列を持っていて、それを反復処理するときに、選択したオブジェクトが null であるかどうかをチェックしてから他の処理を行おうとしました。しかし、nullかどうかをチェックする行為でさえ、NullPointerExceptionを通過するようです。また、array.lengthはすべてのnull要素を含んでしまいます。配列の null 要素のチェックはどのように行うのでしょうか?例えば、以下のコードでは、NPEをスローします。

Object[][] someArray = new Object[5][];
for (int i=0; i<=someArray.length-1; i++) {
    if (someArray[i]!=null) { //do something
    } 
}
質問へのコメント (2)
ソリューション

あなたが言った以上のことが起こっているのです。 あなたの例から、次のような拡張テストを実行しました。


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
解説 (2)

ありません。

以下を参照してください。 投稿したプログラムは想定どおりに実行されます。

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; ijavac 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)
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要素を見つける場合は、上記のようにループを使用する必要があります。 .

解説 (0)

例のコードはNPEをスローしません。 (i ++の後ろに「;」もあってはなりません)。

解説 (0)

与えられたコードは私のために動作します。 配列の2番目の次元を初期化していないので、someArray[i]は常にnullであることに注意してください。

解説 (0)

まず、そのコードはコンパイルできません。

i++の後の余分なセミコロンを削除したら、私の場合、コンパイルしてうまく実行できるようになりました。

解説 (1)

コードがコンパイルされているかどうかと戦う6e 5の配列を作成して2つの値を追加し、それらを印刷すると、2つの値が表示され、他の値はnullになります。 問題は、サイズは5ですが、配列には2つのオブジェクトがあります。 . 配列に存在するオブジェクトの数を見つける方法。

解説 (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)

1行のコードで実行できます(配列宣言なし)。

object[] someArray = new object[] 
{
    "aaaa",
    3,
    null
};
bool containsSomeNull = someArray.Any(x => x == null);
解説 (0)