- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
前几天,我们在这段代码的应用程序日志中发现了一个相当尴尬的异常:
final LIST currentSinks = this.sinks;
if (null != currentSinks && !currentSinks.isEmpty())
{
for (final DataSink<? super T> sink : currentSinks)// **the exception is thrown here**
{
sink.updated(oldValue, newValue);
}
}
LIST 在哪里
public abstract class AbstractDataSource<T, LIST extends Collection<DataSink<? super T>>>
使用的实现是ArrayList。它只是抛出 NoSuchElementException .老实说,我真的不明白为什么。无论我尝试什么,都得到了预期的行为:没有通过 for 进行迭代。
堆栈跟踪的第一行:
java.util.NoSuchElementException caught here: java.util.ArrayList$Itr.next(ArrayList.java:834)
任何提示或解释将不胜感激。
编辑可能一个好的起点是隔离 UT 中的行为。对此有什么想法吗?
最佳答案
好吧,我会努力总结评论中讨论的内容。谢谢大家,请随意编辑此答案。
错误描述感觉是并发错误,所以我们的理论是:
probably another thread removed an element between the Iterator's
hasNext()
andnext()
call, that happens behind the scenes in thefor(:)
loop (thanks Steve!).
那么,让我们来检验我们的理论:
1) 首先,list
类型是 ArrayList
并且从 ArrayList 文档我们有:
ArrayList is not synchronized, so if you need multi-threaded access, consider using
List l = Collections.synchronizedList(new ArrayList(...));
2) 然后我们检查源代码以查找对 NoSuchElementException
的引用:在 java.util.ArrayList.java
上未找到任何事件, 但在 java.util.AbstractList.java
中出现了五次,其中最有趣的是:
// under: public ListIterator<E> listIterator(final int index) {
// return new ListIterator<E>() {
// line 1040:
/**
* Retrieves the next object from the list.
*
* @return The next object.
* @throws NoSuchElementException if there are no
* more objects to retrieve.
* @throws ConcurrentModificationException if the
* list has been modified elsewhere.
*/
public E next()
{
if (position == size)
throw new NoSuchElementException();
position++;
return i.next();
}
回到我们的理论,我们可以说:
ArrayList
未同步 (1),在任何并发修改检查之前,iterator.next()
会抛出 NoSuchElementException
(2).希望能帮到您解决问题!
关于Java foreach NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594002/
此查询由 2 个相关问题组成。在进行下一步之前,我需要等待元素不可见,因此我尝试定义一个自定义方法,如下所示: public void waitToDisappear(long timeOutInSe
所以我有一个一年前编写的相当大的 java 应用程序,我试图再次理解它。我正在查看代码中的一个方法,其中明显存在出现 NoSuchElementException 的风险:我正在使用任意字符串构造的扫
我开始通过简单地读取数据文件来做练习。当我运行该程序时,数据文件被读取但没有读取,但出于某种原因我仍然收到“NoSuchElementException”并且我的输出没有按照预期的方式格式化。这是正在
我正在尝试逐行读取一个大型 csv 文件,以查找其中字符串出现的次数。 这是执行此操作的代码: public int getOffset(File file, String searched) thr
我的代码在使用扫描仪的每一行上都给出了 NoSuchElementException 异常。我的其余代码工作正常。 import java.io.*; import java.util.*; impo
当我运行我的程序时,我在 Eclipse 控制台中得到了这个: Type a number then press enter. 1. Add a customer 2. Add an order 3.
我想知道是否有人可以帮助解决我的程序中的 NoSuchElements 异常,该程序扫描非常大的文本,然后将其添加到 ArrayList 中。 我尝试重新排列代码的顺序,看看是否可以修复它,但现在我不
for (final ArrayList smartPhones : smartPhonesCluster) { new Thread(new Runnable() { @Ov
我想在客户端读取时设置超时。该例程应该抛出 InterruptedIOException,但它在 System.out.println("echo: " + _in.nextLine()); 上抛出
我在功能检查冲突方面遇到问题。无论我做什么,它都会抛出 NoSuchElementException。这是: public boolean isColliding(Pixel a) { if(
如果在读取文件时缺少字段,我应该捕获 NoSuchElementException。如果缺少一个字段,我只需要跳到文件的下一行。我的问题是,我在哪里实现我的 try/catch 代码来做到这一点?这是
我正在开发一种解析器(业余爱好项目),它采用 Cpp 文件,读取文件中的注释,然后尝试基于该文件创建头文件。 我面临的问题是当java.util.Scanner即将读取第一行时。程序停止并给出 NoS
我的任务是: 创建一个包含 2 列数字的文件:距离和速度。 编写一个 TravelInfo 类,其中包含三部分信息:速度、时间、距离。 该类还应该有一个方法 calcTime(),用于根据距离和速度计
在过去的几个小时里,我一直对迭代器感到震惊。 当我执行Iterator时,得到NoSuchElementException 代码 new Thread() { @Overrid
我尝试了许多不同的方法来修复该错误。我知道这与 hasNext 有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从 getLargestQuantityItem 方法将变量larges
代码在 Gitbash 命令行 (GitHub) 中正确运行。但是,当我在 NetBeans 中编译并运行它时,它给出了 NoSuchElement Exception。如果有人可以帮助我使用 Net
从队列中删除时,我有时会收到 NoSuchElementException 。我需要使用锁/等待/其他一些延迟机制吗? 我提供了代码的粗略翻译。 带队列的线程 public void run(){ w
如何删除代码中的 NoSuchElementException ?我也在应用 hasNextLine 但它也没有接受它。 public static void main (String[] a
我已经做了一些研究,但一无所获。 这是我的代码: public void play(){ System.out.print("Make your choice ( R - P - S) : "
自从我醒来以来,我一直在尝试让它工作,但我根本无法弄清楚是什么导致了这个异常。 try { scan = new Scanner(new File("dictionary.txt"))
我是一名优秀的程序员,十分优秀!