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

user2811419
  • 1,923
  • 2
  • 14
  • 15
  • http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html – OldProgrammer Oct 29 '13 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`. – Jesper Oct 29 '13 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. – Abhijith Nagarajan Oct 29 '13 at 14:16
  • Take a look a this http://stackoverflow.com/a/38998115/3380878 – Mojtaba Yeganeh Aug 17 '16 at 13:32

2 Answers2

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()
}
Kal
  • 24,724
  • 7
  • 65
  • 65
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.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
  • I think he has two problems `samples` and `orders` inside the samples loop he are changing the content of orders too. – Jorge Campos Oct 29 '13 at 14:17
  • 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 :) – Guillaume Oct 29 '13 at 14:20
  • 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? – user2811419 Oct 29 '13 at 14:22
  • or do I have to reconstruct this whole thing from scratch – user2811419 Oct 29 '13 at 14:23
  • 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) – Guillaume Oct 29 '13 at 14:23
  • sorry I am just a bit confused as to how to fix this then? – user2811419 Oct 29 '13 at 14:25
  • While inside a for loop, you cannot modify the collection you're doing the loop on (removing or adding elements). Instead, you can build a new collection that you declare before the loop, and you stick the elements you want in. – Guillaume Oct 29 '13 at 14:27
  • Or you can use the solution provided by Kal, it would probably work, but I still think it's bad practice to modify your collection while iterating it. – Guillaume Oct 29 '13 at 14:29
  • by build a new collection, you mean to create two new empty sets and a new list, and input what I want into those lists by looping through my existing maps? – user2811419 Oct 29 '13 at 14:33