I am a beginner at Java and game programming. I am working on a Java 2D game and adding an inventory and looting system. Basically, when my player picks up an item, I want to remove the physical object from the world. However, I am getting a ConcurrentModificationException error. Here is a part of my code (which I think may be part of the problem so I will include the code):
for (GameObject obj : goManager.gameObjects) {
    if (obj instanceof ItemObject itemObj) {
        if (bounds().intersects(itemObj.getBounds())) {
            collidingItem = itemObj;
        } else {
            collidingItem = null;
        }
    }
}
And this is the code where the error happens:
inventory.add(collidingItem.getItem());
goManager.gameObjects.remove(collidingItem); // Error happens here
collidingItem = null;
The output:
Exception in thread "Thread-0" java.util.ConcurrentModificationException
    at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1013)
    at java.base/java.util.ArrayList$Itr.next(ArrayList.java:967)
    at com.yiwuen.PixelBlock.entity.GameObjectManager.render(GameObjectManager.java:12)
    at com.yiwuen.PixelBlock.Game.render(Game.java:146)
    at com.yiwuen.PixelBlock.Game.run(Game.java:103)
    at java.base/java.lang.Thread.run(Thread.java:833)
I'm not sure how to remove the colliding item from the ArrayList because when I do so, that error occurs. Any thoughts?
I've also tried to just set the colliding item to null but that obviously didn't work. I researched but I didn't understand any of the solutions.
 
     
    