title: iterator hasnext() returns true but next() throws NoSuchElementException
body: <p>When I debugged my code I found that the <code>hasNext()</code> method of <code>Iterator</code> returned <code>true</code>, but the <code>next()</code> method threw NoSuchElementException.</p>
  
  <p>Below is my code:</p>
  
  <pre><code> Collection&lt;TradeStock&gt; 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();
      }
  }
  </code></pre>
  
  <p>What am I getting wrong?</p>