iterator hasnext()返回true但next()抛出NoSuchElementException

当我调试我的代码时,我发现IteratorhasNext()方法返回true ,但next()方法抛出了NoSuchElementException。

以下是我的代码:

  Collection restBuy=em.createQuery("select t from TradeStock ...t.getResultList(); if(!restBuy.isEmpty()) { Iterator itrest=restBuy.iterator(); while(itrest.hasNext()) { TradeStock ts=(TradeStock)itrest.next(); x+=ts.getTradeExecutedQuantity(); } } 

我错了什么?

当我重新启动服务器并重新部署我的应用程序时,代码工作,所以代码没有错,我猜,我无法理解这种奇怪的玻璃鱼行为!

当您说“调试我的代码”时,您是指使用调试器进行调试,就像在Eclipse中一样?
如果您的计算表达式(Eclipse中的“ 表达式”选项卡)包含itrest.next(),则调试器将调用next()方法并修改Iterator的状态,而无需您的代码知道它。
尝试调试此方法,而无需评估itrest.next()或日志消息