简体   繁体   English

在代码中获取 java.util.NoSuchElementException。 为什么?

[英]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.

相关问题 获取 java.util.NoSuchElementException - Getting java.util.NoSuchElementException Java扫描程序:获取“java.util.NoSuchElementException”错误,不确定原因 - Java scanner: Getting “java.util.NoSuchElementException” error, not sure why 为什么会收到“ java.util.NoSuchElementException”? - Why am I getting a “java.util.NoSuchElementException”? 不知道为什么我收到java.util.NoSuchElementException错误 - Not sure why i'm getting an java.util.NoSuchElementException error 为什么我从下面的代码中收到错误 java.util.NoSuchElementException? - Why am I getting the error java.util.NoSuchElementException from my code below? 我执行以下代码时,发生“ main”线程中的“ java.util.NoSuchElementException”异常。 - “ Exception in thread ”main“ java.util.NoSuchElementException ” error is occured when i execute the following code. Java代码中的java.util.NoSuchElementException - java.util.NoSuchElementException in Java code 继续在此程序上获取java.util.NoSuchElementException - Keep getting the java.util.NoSuchElementException on this program java.util.NoSuchElementException - java.util.NoSuchElementException 为什么我在在线编码 class 代码检查器上收到“线程中的异常”主“java.util.NoSuchElementException”错误? - Why am I getting a “Exception in thread ”main“ java.util.NoSuchElementException” error on a online coding class code checker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM