Java 检索数组列表的大小时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3754985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:27:24  来源:igfitidea点击:

Error in retrieving the size of an array list

javaarraylist

提问by Anto

I am creating an application which retrieve from an external party application an arraylist of Integer values. It seems that some of the elements in this retrieved list are empty or null. When I retrieve the size of the list for example:

我正在创建一个应用程序,它从外部方应用程序检索整数值的数组列表。这个检索到的列表中的某些元素似乎是空的或为空的。例如,当我检索列表的大小时:

System.out.println("The number of elements is : "+ elements_list.size());

it just throws an null pointer exception. Does anyone know a solution for such a problem? Thanks in advance!

它只是抛出一个空指针异常。有谁知道此类问题的解决方案?提前致谢!

采纳答案by Robin

This means that the list you have received is null, it has nothing to do with its elements.

这意味着您收到的列表为空,与它的元素无关。

回答by RHSeeger

If

如果

elements_list.size()

is throwing a null pointer exception, then the problem isn't that there are null items in the list. Rather, the call you're making to get the list is returning null (ie, not returning an actual list).

抛出空指针异常,那么问题不在于列表中有空项。相反,您为获取列表而进行的调用将返回 null(即,不返回实际列表)。

I'll admit i find it distasteful to return null when the code should be returning a list. Most of the time, the code should return the list (if it got values), an exception (if there was an error generating the values), or a empty list (if there were no values).

我承认我觉得当代码应该返回一个列表时返回 null 令人讨厌。大多数情况下,代码应该返回列表(如果有值)、异常(如果生成值时出错)或空列表(如果没有值)。

回答by Nathan Hughes

It's not complaining that a list element is null, it's complaining that the elements_list object itself is null. Check that elements_list is non-null before you do anything with it.

它不是在抱怨列表元素为空,而是在抱怨 elements_list 对象本身为空。在对元素列表执行任何操作之前,请检查它是否为非空。

回答by sdavids

Your elements_list is null, so when you call the method size() it throws an exception. You can check if the list is null first by doing:

您的 elements_list 为空,因此当您调用方法 size() 时,它会引发异常。您可以先检查列表是否为空:

System.out.println("The number of elements is : " + elements_list == null ? 0 : elements_list.size());

回答by Mike G

In the case of a NullPointerException in that code, the list itself is null. Wherever elements_list is being set, it is coming in as a null value. Check with your external party application and double-check any code that might be manipulating elements_list in between.

在该代码中出现 NullPointerException 的情况下,列表本身为空。无论在何处设置 elements_list,它都会以空值的形式出现。检查您的外部方应用程序,并仔细检查可能在其间操作 elements_list 的任何代码。

回答by Romain Linsolas

elements_listis null, so you can't call any method on it. The best way to do your test is to check both nullability and emptiness:

elements_listis null,所以你不能在它上面调用任何方法。进行测试的最佳方法是检查可空性和空性:

if (elements_list == null) || elements_list.isEmpty()) {
    ...
} else {
    ...
}

回答by desvin

I had the same problem.

我有同样的问题。

if (list==null || list.size()==0) //throws exception

The list is not nullbut the size cannot be determined. The problem was that the collection was read from the DB with 'lazy' on. Hence it was not totally read and so The .size()call throws a 'NullPointerException'. Reading the entire collection takes care of the problem.

该列表不是,null但无法确定大小。问题是该集合是从数据库中读取的“懒惰”。因此它没有被完全读取,所以.size()调用会抛出一个“NullPointerException”。阅读整个集合可以解决这个问题。