MongoDB Java Driver Infinite loop while reading results

I am reading MongoDB results using the following code , however the while loop runs in an infinite loop always iterating over the first element in the collection, can someone point out what I am doing wrong.

       Iterable<DBObject> list = playerData.results();
        if(list != null){
            while(list.iterator().hasNext()) {

                DBObject obj = list.iterator().next();
                DBObject id =  (DBObject) obj.get("_id");
                String player= obj.get("player").toString();
                //Populate the memcached here .
                PlayerDTO rcd = new PlayerDTO();

                if(id != null && id.get("venue" != null && id.get("score") != null) {

                    rcd.setVenue(id.get("venue").toString());
                    rcd.setScore(new Double(id.get("score").toString()).doubleValue());
                }

            }
        }

Answer

You are reassigning the original iterator to the while() loop each iteration through.

Iterator i = list.iterator();
while(i.hasNext()) {
  ....
}

Leave a Reply

Your email address will not be published. Required fields are marked *