6

My code results in an error where I do not know how to fix it. I tried putting in print statements but It wont even make it that far. The error occurs

Here is the exact error

java.util.ConcurrentModificationException
java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(HashMap.java:894)
        at java.util.HashMap$KeyIterator.next(HashMap.java:928)
        at ca.on.oicr.pinery.lims.gsle.GsleClient.getOrders(GsleClient.java:720)

Line 720 is the second for loop

4
  • docs.oracle.com/javase/7/docs/api/java/util/… Commented Oct 29, 2013 at 14:14
  • 3
    You're adding to orders inside a loop that's looping over the elements of orders, that's what causes the exception. Don't modify a collection you're looping over inside the loop. Likewise with samples. Commented Oct 29, 2013 at 14:15
  • You cannot modify (add or remove) the collection when you are looping over it. You are trying to add a order to orders. You cannot do that. Commented Oct 29, 2013 at 14:16
  • Take a look a this stackoverflow.com/a/38998115/3380878 Commented Aug 17, 2016 at 13:32

2 Answers 2

3

You can use a ListIterator if you want add or remove elements from a list while iterating over the elements. This is assuming that your orders is a List

So, your code would look something like this --

ListIterator<Order> it = orders.listIterator();

while ( it.hasNext() ) {
      Order ord = it.next();

      if ( ) // some condition
        it.remove(); // This wil remove the element that we just got using the next() method
      if ( ) // some other condition
        it.add(new Order()); // THis inserts the element immediately before the next call to next()
}
Sign up to request clarification or add additional context in comments.

2 Comments

in my case, would I be removing? and I am creating a new instance of Order everytime?
If the orders is a List, then you can use the ListIterator and use the add method
1

You're trying to manipulate the content of sample while iterating over its contents. To fix this kind of problems, use immutable collections, or pretend they are.

What you want to do is, while iterating over samples, build up another collection with the one you want, and modify this other collection instead of your original one.

9 Comments

I think he has two problems samples and orders inside the samples loop he are changing the content of orders too.
Yes, I wish that default collections in Java were immutable, to prevent this kind of behaviour... (Like they are in Scala) - we need more functional programming concepts into our OO ways :)
to fix this would I just put the orders.add(order) outside the second forloop but within the first first loop? would that solve it?
or do I have to reconstruct this whole thing from scratch
You would have to rewrite it: You cannot modify a collection while iterating over it! The problem would still exist if you had only one collection (orders)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.