我有一个我无法理解的问题。元素在foreach循环中为null
有一个for-each循环,调试器告诉我列表中有值,但是具体的body-loop表示当前元素为null。
这怎么可能?
public void test(){
List cs = ["a"];
for(String c:cs){
print c; // but c is null(sais the debugger)! The console shows "null".
}
}
编辑:另一个occourence是:
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 但是:
List<StaticSubFoobary> getBackendSubFoobaryList(List<String> electedSubFoobaryIds) {
List<StaticSubFoobary> subFoobaries = getStaticBackendFoobaryList()?.collect { StaticMainFoobary cat -> cat.backendSortedSubFoobaries }?.flatten()
List<StaticSubFoobary> electedSubFoobaries = []
for(StaticSubFoobary it:subFoobaries)
if(it.numericId in electedSubFoobaryIds)
electedSubFoobaries.add(it)
return electedSubFoobaries
}
不!
你可以粘贴你正在初始化列表的完整代码吗? – NINCOMPOOP 2012-02-21 11:05:21
@noob *咳嗽*'列表cs = [“a”];' – bezmax 2012-02-21 11:06:29
这是Java吗?如果没有适当标记。 – sgowd 2012-02-21 11:06:47