[英]Getting java.util.NoSuchElementException in the code. Why?
I have the following code given below :我有以下代码:
final LinkedHashMap<String, Integer> linkedHashMapCounterMap = new LinkedHashMap<>();
linkedHashMapCounterMap.put("abc", 2);
linkedHashMapCounterMap.put("xyz", 5);
final String maxColln = Collections.max(
linkedHashMapCounterMap.entrySet(),
(entry1, entry2) -> entry1.getValue().intValue() -
entry2.getValue().intValue())
.getKey();
After running the code, I'm getting this exception :运行代码后,我收到此异常:
java.util.NoSuchElementException: null at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:721) ~[?:1.8.0_202] at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:752) ~[?:1.8.0_202] at java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:750) ~[?:1.8.0_202] at java.util.Collections.max(Collections.java:708) ~[?:1.8.0_202] java.util.NoSuchElementException:在 java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:721) ~[?:1.8.0_202] 在 java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:752) 处为 null [?:1.8.0_202] 在 java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:750) ~[?:1.8.0_202] 在 java.util.Collections.max(Collections.java:708) ~[? :1.8.0_202]
There is an edge-case bug in the comparator lambda, but that shouldn't cause an NPE.比较器 lambda 中存在边缘情况错误,但这不应导致 NPE。 (Use Integer.compareTo(Integer)
: see Java Integer compareTo() - why use comparison vs. subtraction? ) (使用Integer.compareTo(Integer)
:请参阅Java Integer compareTo() - 为什么使用比较与减法? )
I think that the NPE must be due to a threading bug;我认为 NPE 一定是由于线程错误; eg there is another thread modifying the map while this code is running.例如,在此代码运行时,有另一个线程在修改地图。 I cannot see how this code could NPE in next()
, in any other way.我看不出这段代码如何在next()
中以任何其他方式 NPE 。
Either way, we need a minimal reproducible example to take this any further.无论哪种方式,我们都需要一个最小的可重复示例来进一步研究。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.