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

后端 未结 9 1047
情深已故
情深已故 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

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

提交回复
热议问题