Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a 2D and currently working on shooting with bullets. The bullet is a seperate class. All the bullets are stored in an arraylist called bullets. I am trying to make it destroy itself when it is out of the side of the screen (< -16), but when I try it gives me this error.

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at GameCanvas.updateMovement(GameCanvas.java:94)
at GameCanvas.gameLoop(GameCanvas.java:63)
at GameCanvas.<init>(GameCanvas.java:28)
at GameClient.<init>(GameClient.java:68)
at GameClient.main(GameClient.java:29)

I assume it has something do with when it is being destroy, I am using this code to destroy it:

public void move() {

    if(x > -16) {
        x -= move_speed;
    } else {
        kill();
    }

}

public void kill() {
    ObjectHandler.bullets.remove(this);
}

updateMovement() method:

public void updateMovement() {
    PlayerObject.update();

    for(Bullet bullet : ObjectHandler.bullets) {
        bullet.move();


    }

}

Why is it doing this? Thanks.

share|improve this question
 
We probably need to see the updateMovement source to help you here. –  Louis Wasserman Feb 24 '12 at 19:56
 
done, sorry about that. –  Duncan Palmer Feb 24 '12 at 19:57

4 Answers

up vote 4 down vote accepted

This happens because you are modifying a list while you're iterating over it.

Try to "remember" what you need to kill and after you've passed the whole list, go through your "memory" and perform the appropriate kills. This way you will modify the list after iterating over it.

E.g.:

public void move() {

    if(x > -16) {
        x -= move_speed;
    } else {
        ObjectHandler.remember_to_kill(this);
    }

}

// in ObjectHandler:

private HashSet<type_of_object_to_kill> kill_memory = new...;

public void remember_to_kill() {
    this.kill_memory.add(this);
}

private void kill_from_memory() {
    for (type_of_object_to_kill obj: kill_memory) {
        ObjectHandler.bullets.remove(this);
    }
    ObjectHandler.kill_memory.clear();
}

// update movement:

public void updateMovement() {
    PlayerObject.update();

    for(Bullet bullet : ObjectHandler.bullets) {
        bullet.move();
    }
    ObjectHandler.kill_from_memory();
}
share|improve this answer
 
Thanks, yea I used another ArrayList to store the bullets out of range, then destroyed them after the movement loop. Thanks alot :) –  Duncan Palmer Feb 24 '12 at 20:03
 
Also, consider using one of the concurrent collections, they solve this problem automatically (though at the cost of being thread-safe). –  Irfy Feb 24 '12 at 20:05

From the stack trace we can see you have a method that gameLoop() that calls updateMovement() in GameCanvas. The latter iterates over bullets in main thread. At the same time your kill() method modifies the list. This is exactly what the exception says and prevents you from further damage.

You have to synchronize access to bullets list or use thread-safe collection like CopyOnWriteArrayList.

share|improve this answer

This is usually thrown when you are iterating through a list and trying to remove/modify an item from the list at the same time. I'm not sure exactly what is going on. More code examples would be helpful.

share|improve this answer

The problem is that you're trying to modify the collection removing an element while you're reading at the same time. Of course this might be dangerous and so the exception is thrown. If you use an iterator you could leverage its functions to remove the current element.

Something like this:

public void updateMovement() {
    PlayerObject.update();

    Iterator<Bullet> iterator = ObjectHandler.bullets.iterator();
    while(iterator.hasNext()) {
        Bullet bullet = iterator.next();
        if(x > -16) {
            x -= move_speed;
        } else {
            iterator.remove();
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.