Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterator hasnext() returns true but next() throws NoSuchElementException

When I debugged my code I found that the hasNext() method of Iterator returned true, but the next() method threw NoSuchElementException.

Below is my code:

 Collection<TradeStock> 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();
    }
}

What am I getting wrong?

like image 749
z22 Avatar asked Sep 15 '25 02:09

z22


2 Answers

When you say "debugged my code" do you mean debug using a debugger, like in Eclipse?
If your evaluated expression (Expressions tab in Eclipse) includes itrest.next() then the debugger invokes the next() method and modifies the state of your Iterator, without your code being aware of it.
Try debugging this either without evaluating itrest.next() or with log messages

like image 181
Oded Peer Avatar answered Sep 17 '25 17:09

Oded Peer


the code worked when i restarted the server and redeployed my app, so nothing was wrong with the code i guess, i cant understand this strange behavior of glassfish!

like image 33
z22 Avatar answered Sep 17 '25 16:09

z22