-1

This is my code:

Iterator<H> iter = ((Main) getOwner()).eH.iterator();
    while (iter.hasNext()) {
        if (iter.next().z.c > p_l) {
            if (r) {
                if (iter.next().R) {
                    if (iter.next().p <= 0.7 * s && iter.next().c_l >= p_l) {
                        if (s_h == null) {
                            s_h = iter.next();
                        } else {
                            if (iter.next().p <= s_h.price) {
                                s_h = iter.next();
                            }
                        }
                    }
                }
            }
        }
    }

but I am getting this error:

Error during model startup:
java.util.NoSuchElementException
java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:839)
    at d_w.P.I(P.java:681)
    at d_w.P.checkIfI(P.java:649)
    at d_w.Main.initModelStructure(Main.java:1072)
    at d_w.Main.onStartup(Main.java:2775)
    at d_w.Main.start(Main.java:2765)
    at com.anylogic.engine.Engine.start(Unknown Source)
    at com.anylogic.engine.ExperimentSimulation.r(Unknown Source)
    at com.anylogic.engine.ExperimentSimulation.run(Unknown Source)
    at d_w.Simulation.executeShapeControlAction(Simulation.java:111)

I really not sure why I am getting this error any suggestion is highly appreciated thanks in advance

npinti
  • 51,780
  • 5
  • 72
  • 96
jone kemberly
  • 13
  • 1
  • 5
  • 1
    **and What exactly you want to achieve with this piece of code !** – Neeraj Jain May 27 '15 at 09:44
  • this is just a part of bigger code I am trying to simulate the behavior of an agent to find a house from available houses sorry I didn't used the full word because its an exam and I don't want my student to see the code :) – jone kemberly May 27 '15 at 09:46
  • you are calling itr.next() so many times , make sure you have enough number of items in your collection to which you have attached the iterator . – Alok Mishra May 27 '15 at 09:47
  • thanks guys and chetan Kinger – jone kemberly May 27 '15 at 10:09

2 Answers2

0

Calling .next() will retrieve the next element and advances the iterator. You have multiple .next() calls without checking if there is a next element at all.

If you want to keep using the same element that the .next() initially returned, then, replace the first .next() with H next = iter.next(), then access the next variable.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

Every time you call next() you don't get the same element, instead you traverse to the next element of the iterator. You should save this value to a variable and use it.

Forketyfork
  • 7,416
  • 1
  • 26
  • 33