java 如何在java中检查元素是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3745272/
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-10-30 03:14:26  来源:igfitidea点击:

How to check element is not null in java

java

提问by user441978

filterItems = null;
filterItems = new ArrayList<SelectItem>();


Iterator<String> it = RefListMap.keySet().iterator();
        while (it.hasNext()) {
            String s = (String) it.next();
            filterItems.add(new SelectItem(s, s));
        }

Now i am getting filterItems size=1 but that value is null.How to check array element is not equal to null.

现在我得到 filterItems size=1 但该值为空。如何检查数组元素不等于空。

if(filterItems != null)
{

 code
}

but this condition is not working...Please help me.

但这种情况不起作用...请帮助我。

回答by jjczopek

try:

尝试:

String s = (String) it.next();
if(s != null){
    filterItems.add(new SelectItem(s, s));
}

回答by crazyscot

Presumably the value you're getting from the RefListMapis null. You might check whether swas null before you add it to filterItems.

据推测,您从 中获得的RefListMap值为空。您可能要检查是否s为空,你将它添加到以前filterItems