jBox2d querying aabb

Hey again
I must admit I’m feeling pretty dumb - it seems like every couple of minutes I run into another issue with jBox2d, this one is to do with querying the world object’s AABBs and it’s giving me an annoying exception:


Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at morningside.physics.PhysicsManager.queryWorld(PhysicsManager.java:332)
	at morningside.physics.PhysicsManager.processCommand(PhysicsManager.java:295)
	at morningside.main.Console.execute(Console.java:148)

The way I query the AABB is by using two arraylists that I empty after each query:


 public ArrayList<Body> queryWorld(float x1, float y1, float x2, float y2) {
        callbackList.clear();
        bodyList.clear();

        queryAABB = new AABB(new Vec2(x1, y1), new Vec2(x2, y2));
        gWorld.queryAABB(new QueryCallback() {
            @Override
            public boolean reportFixture(Fixture fixture) {
                callbackList.add(fixture);
                return true;
            }
        }, queryAABB);

        for (Fixture f : callbackList) {
            if (!bodyList.contains(f.getBody())) { //the error occurs here
                callbackList.add(f);
            }
        }

        return bodyList;
    }

The error seems to be occurring at the line that I commented - it’s when i try to access the list I’ve populated via the callback methods after I call the query. I’m just not entirely sure what causes this error - is there a way for QueryAABB to block somehow? This is the only place I access that list as of now so I’m sort of stumped.

If you need any more info ask me, again thank you all for your help.

You cannot add to “callbackList” (line 16 in your code listing) while you iterate over it (line 14).


 public ArrayList<Body> queryWorld(float x1, float y1, float x2, float y2) {
        callbackList.clear();
        bodyList.clear();

        queryAABB = new AABB(new Vec2(x1, y1), new Vec2(x2, y2));
        gWorld.queryAABB(new QueryCallback() {
            @Override
            public boolean reportFixture(Fixture fixture) {
                callbackList.add(fixture);
                return true;
            }
        }, queryAABB);

+        List<Fixture> temp = new ArrayList<>();

        for (Fixture f : callbackList) {
            if (!bodyList.contains(f.getBody())) { //the error occurs here
+                temp.add(f);
            }
        }

+       callbackList.addAll(temp);

        return bodyList;
    }

ah, now that makes too much sense for me to have thought of that by myself 8)
thank you very much