Duncan Palmer
Duncan Palmer

Reputation: 2913

Java: java.util.ConcurrentModificationException

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.

Upvotes: 3

Views: 9235

Answers (4)

Marsellus Wallace
Marsellus Wallace

Reputation: 18601

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();
        }
    }
}

Upvotes: 0

Irfy
Irfy

Reputation: 9587

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();
}

Upvotes: 4

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340743

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.

Upvotes: 2

Reid Mac
Reid Mac

Reputation: 2489

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.

Upvotes: 0

Related Questions