Solved Java.util.NoSuchElementException help

Discussion in 'Plugin Development' started by dibujaron, Dec 24, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    dibujaron

    Hello. I'm working on a project that uses iterators. This code below is throwing a NoSuchElementException. Everywhere I can find says that the best fix for a NoSuchElementException is adding a while loop with iterator.hasNext(). However, my code already has a while loop. If anyone could explain to me why this might be happening I would be very grateful.

    the code:
    Code:
                    Iterator<Shipyard> itr = Shipyards.iterator();
                    while (itr.hasNext()){
                        try {
                            itr.next().time --;
                            itr.next().sign.setLine(3, Integer.toString(itr.next().time));
                            itr.next().sign.update();
                            if (itr.next().time <= 0){
                                itr.next().unRent();
                                itr.remove();
                            }
                        } catch (MaxChangedBlocksException e) {
                            e.printStackTrace();
                        }
                    }
     
  2. Offline

    fireblast709

    Its because you call itr.next() multiple times, so in your case you already jump 5 times ahead
     
  3. Offline

    dibujaron

    ah, thanks. So I should do something like Shipyard s = itr.next() and then use that.

    thanks you very much!
     
  4. Offline

    fireblast709

    yes. You might need to cast it to Shipyard though, but your IDE should hint you on that
     
Thread Status:
Not open for further replies.

Share This Page