-2

I don't understand why this method throws an exception:

public void add(Object obj){
    gameObjects.add(obj); //here the exception happens
}

... while this one doesn't:

public void add(Object obj){
    gameObjects.add(obj); // no exception actually happens here 
    gameObjects.remove(obj);
}

Why does this happen, considering that is it a run time exception?

Exception:

Exception in thread "Thread-0" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at threads.Main.tick(Main.java:181)
    at threads.Main.run(Main.java:104)

The method is called for a tick method inside an object.

gameObjects isn't null:

List<Object> gameObjects = new ArrayList<Object>(128);
asteri
  • 11,402
  • 13
  • 60
  • 84
Kacper Lubisz
  • 780
  • 2
  • 10
  • 17
  • 9
    This isn't enough info. First, what is the exception. Also, what is the context of these calls? Who calls the code and how? – Daniel Kaplan Jul 01 '13 at 18:02
  • What type is gameObjects? – Cemafor Jul 01 '13 at 18:02
  • What is `gameObjects` ? Is it null ? – AllTooSir Jul 01 '13 at 18:02
  • ConcurrentModificationException occurs when you modify the list (by adding or removing elements) while traversing a list. – AllTooSir Jul 01 '13 at 18:05
  • Possible duplicate: http://stackoverflow.com/q/8189466/738746 – Bhesh Gurung Jul 01 '13 at 18:05
  • 1
    To all the downvoters and closevoters: it's actually not that bad of a question, if he's asking why adding the `.remove()` call prevents the exception from being thrown. – asteri Jul 01 '13 at 18:09
  • I think this is asking the same thing as my question, in a slightly different way. So I voted to close as a duplicate. But no downvote becase I think it's a good question, even though the way it was posted in the beginning was bad. – Bhesh Gurung Jul 01 '13 at 18:15

4 Answers4

3

Looks like you're trying to add it in a loop. Java does not allow this. If you add and then remove in the same method, you're not really doing much, the net change is no different so you're not trying to change a collection during a loop.

If you want it add it, you'll have to do so using an iterator.

Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
2

The first method throws an exception because you are modifying some collection in a loop. The second does not because there is no net change to the collection; you add then remove an element.

Note that it is not the add method that checks for a ConcurrentModificationException; rather, the collection is checked for any modifications at the end of each loop iteration.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

according to the doc

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

You can't change the same object while traversing.

JavaDoc say's

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

For more details follow these links. They may help you to learn.

Concurrent Modification exception http://docs.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115