3

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.

1
  • We probably need to see the updateMovement source to help you here. Commented Feb 24, 2012 at 19:56

4 Answers 4

4

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, yea I used another ArrayList to store the bullets out of range, then destroyed them after the movement loop. Thanks alot :)
Also, consider using one of the concurrent collections, they solve this problem automatically (though at the cost of being thread-safe).
2

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.

Comments

0

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.

Comments

0

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

Comments

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.