Groovy 元素在foreach循环中为null
我有一个我不能理解的问题 每个循环都有一个for,调试器告诉我列表中有值,但具体的body循环表示当前元素为null 这怎么可能Groovy 元素在foreach循环中为null,groovy,foreach,Groovy,Foreach,我有一个我不能理解的问题 每个循环都有一个for,调试器告诉我列表中有值,但具体的body循环表示当前元素为null 这怎么可能 public void test(){ List cs = ["a"]; for(String c:cs){ print c; // but c is null(sais the debugger)! The console shows "null". } } 编辑:另一种情况是: List<StaticSubFoobary> ge
public void test(){
List cs = ["a"];
for(String c:cs){
print c; // but c is null(sais the debugger)! The console shows "null".
}
}
编辑:另一种情况是:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = subFoobaries.findAll { it.numericId in electedSubFoobaryIds}
return electedSubFoobaries
}
不要扔NPE
但是:
没有
但是具体的body循环表示当前元素为null
不,没有。如果在Groovy控制台中运行此代码,则断言将传递:
List cs = ["a"];
for(String c:cs){
assert c == 'a'
}
相反,如果在控制台中运行此代码,则断言将失败
List cs = ["a"];
for(String c:cs){
assert c == null
}
这最终证明了列表的第一个元素是一个非空的元素,试图将GroovyString转换为字符串可能是个问题。尝试使用单引号字符串文字。我终于再次签出了该项目,并像以前一样构建了它 问题消失了,不知道为什么,但它现在起作用了 我只需将所有更改复制到新工作区并继续开发
让此解决方法成为经验的一部分。您可以将完整代码粘贴到初始化列表的位置吗?@noob cough List cs=[a];这是Java吗?如果没有正确地标记它。@sans481不,它是GroovyIt再次发生。我会开始悬赏的。不,不是吗?是的,是的!调试器sais c为空,我编辑我的问题以强制回答“是”。@PeterRader不相信IntelliJ告诉你的一切。如果您确信c==null,那么如何解释在Groovy控制台中运行这些代码段的结果。我只能通过c='a'来解释它,但是Grails在c.toString上抛出了一个NPE。print c==null表示为真。@PeterRader不,它不是。我猜您编写了错误的代码块作为示例。查看此查看原始问题中运行代码的结果:完整的原始代码是almoste secret抱歉,请检查从我的问题中删除的屏幕截图。
List cs = ["a"];
for(String c:cs){
assert c == null
}