如何检查数组元素是否为null以避免Java中的NullPointerException

如何检查数组元素是否为null以避免Java中的NullPointerException,java,arrays,exception-handling,nullpointerexception,Java,Arrays,Exception Handling,Nullpointerexception,我有一个部分未填充的对象数组,当我遍历它们时,我试图在对其执行其他操作之前检查所选对象是否为null。然而,即使是检查它是否为null的行为也似乎是通过NullPointerException实现的数组。长度也将包括所有null元素。如何检查数组中的null元素?例如,在下面的代码中将为我抛出一个NPE Object[][] someArray = new Object[5][]; for (int i=0; i<=someArray.length-1; i++) { if (so

我有一个部分未填充的对象数组,当我遍历它们时,我试图在对其执行其他操作之前检查所选对象是否为
null
。然而,即使是检查它是否为
null
的行为也似乎是通过
NullPointerException
实现的<代码>数组。长度也将包括所有
null
元素。如何检查数组中的
null
元素?例如,在下面的代码中将为我抛出一个NPE

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

对于(int i=0;i,您所做的事情比您所说的要多。我根据您的示例运行了以下扩展测试:

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

您可能正在尝试检查someArray[index]的长度吗?

给定的代码对我有效。请注意,someArray[i]始终为空,因为您尚未初始化数组的第二维度。

首先,这些代码无法编译

在i++之后删除多余的分号后,它对我来说编译并运行良好。

它没有

请参阅下面。您发布的程序按预期运行

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>
C:\oreyes\samples\java\arrays>type ArrayNullTest.java
公共类ArrayNullTest{
公共静态void main(字符串[]args){
Object[]someArray=新对象[5][];
对于(int i=0;ijavac ArrayNullTest.java)
C:\oreyes\samples\java\arrays>java ArrayNullTest
0处的元素为null
1处的元素为空
位于2的元素为空
3处的元素为空
4处的元素为空
C:\oreyes\samples\java\arrays>

示例代码不会抛出NPE。(在i++后面也不应该有“;”)

与代码是否正在编译作斗争我想说创建一个包含六个值的数组5添加两个值并打印它们,您将得到这两个值,其他值为空。问题是虽然大小为5,但数组中有两个对象。如何查找数组中存在多少对象

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");
  }
如果要查找特定的空元素,应如上所述使用for循环。

publicstaticvoidmain(字符串s[])
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;

}
{ int firstArray[]={2,14,6,82,22}; int secondArray[]={3,16,12,14,48,96}; int number=getCommonMinimumNumber(第一个数组,第二个数组); System.out.println(“编号为”+编号); } 公共静态int getCommonMinimumNumber(int firstSeries[],int secondSeries[] { 整数结果=0; if(firstSeries.length!=0&&secondSeries.length!=0) { 系列(第一系列); 系列(第二系列); 一:for(int i=0;i编号[i+1]) { 温度=编号[i+1]; 编号[i+1]=编号[i]; 编号[i]=温度; 固定=假; } } } /*for(int i=0;i
您可以在一行代码中完成(无需数组声明):


您的代码没有给我NPR。您可能还想使用“如果您选中
!someArray.equals(null)
,iIt将抛出NPE”。
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;

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