Java ConcurrentModificationException

by voltage   Last Updated February 13, 2018 11:13 AM

Hey guys i am trying to solve the problem for 2 days now and dont know how to handle it. i tried to use an iterator also a copy ArrayList but it didnt solved my problem, below are my two relevant classes.I wanna check if my player is colliding with a gem and ig it is colliding then add the gem to item ArrayList and delete it from objects ArrayList.

public class Player extends GameObject {

// bereite und höhe des Rechtecks der das Object umhüllt für Collisin in
// getBounds
private float width = 32, height = 64;
private float gravity = 0.02f;
Handler handler;
Texture tex = Game.getInstance();
private Animation playerA, playerD, playerW, playerS;

public Player(float x, float y, Handler handler, ObjectID id) {
    super(x, y, id);
    this.handler = handler;
    playerW = new Animation(10, tex.playerW.get(1), tex.playerW.get(2));
    playerS = new Animation(10, tex.playerS.get(1), tex.playerS.get(2));
    playerA = new Animation(10, tex.playerA.get(1), tex.playerA.get(2));
    playerD = new Animation(10, tex.playerD.get(1), tex.playerD.get(2));
    score = 0;

}

public void playerMovement() {
    x += velX;
    y += velY;

    if (jumping || falling) {
        velY += gravity;

    }

}

@Override
public void tick(ArrayList<GameObject> object) {
    facingFigur();
    playerMovement();
    collision(object);
    playerW.runAnimation();
    playerS.runAnimation();
    playerA.runAnimation();
    playerD.runAnimation();

}

@Override
public void render(Graphics g) {
    g.setColor(Color.BLUE);
    if ((this.x > -Camera.getX() + Game.WIDTH) || (this.x < -Camera.getX() - 32)
            || (this.y > -Camera.getY() + Game.HEIGHT) || (this.y < -Camera.getY() - 32)) {
    } else {
        if (velX != 0 || velY != 0) {
            switch (facing) {

            case 1:
                playerD.drawAnimation(g, (int) x, (int) y, 24, 32);
                break;
            case -1:
                playerA.drawAnimation(g, (int) x, (int) y, 24, 32);
                break;
            case 2:
                playerW.drawAnimation(g, (int) x, (int) y, 24, 32);
                break;
            case -2:
                playerS.drawAnimation(g, (int) x, (int) y, 24, 32);
                break;
            }
        } else {

            switch (facing) {

            case 1:
                g.drawImage(tex.playerD.get(0), (int) x, (int) y, null);
                break;
            case -1:
                g.drawImage(tex.playerA.get(0), (int) x, (int) y, null);
                break;
            case 2:
                g.drawImage(tex.playerW.get(0), (int) x, (int) y, null);
                break;
            case -2:
                g.drawImage(tex.playerS.get(0), (int) x, (int) y, null);
                break;
            }

        }
    }

}

// Collision

private void collision(List<GameObject> object) {

    for (GameObject objectt : handler.objects) {

        if (objectt.getID().equals(ObjectID.Tree) || objectt.getID().equals(ObjectID.Block)) {

            if (getBoundsTOP().intersects(objectt.getBounds())) {
                y = objectt.getY() + (height / 2);
                velY = 0;

            }

            if (getBounds().intersects(objectt.getBounds())) {
                y = objectt.getY() - height;
                velY = 0;

            } else {

            }

            if (getBoundsRIGHT().intersects(objectt.getBounds())) {
                x = objectt.getX() - width;

            }
            if (getBoundsLEFT().intersects(objectt.getBounds())) {
                x = objectt.getX() + width;

            }

        }

    }

    for (GameObject objectt : handler.objects) {
        if(objectt.getID().equals(ObjectID.Gem)) {
            if(getBoundsTOP().intersects(objectt.getBounds())) {
                handler.collectedItems.add(objectt);
            }
        }

    }
    handler.objects.removeAll(handler.collectedItems);

}

public void facingFigur() {
    if (velX < 0) {
        facing = -1;
    } else if (velX > 0) {
        facing = 1;
    }

    if (velY < 0) {
        facing = 2;
    } else if (velY > 0) {
        facing = -2;
    }

}

public Rectangle getBounds() {
    return new Rectangle((int) ((int) x + (width / 2) - width / 4), (int) ((int) y + (height / 2)), (int) width / 2,
            (int) height / 2);
}

public Rectangle getBoundsTOP() {
    return new Rectangle((int) ((int) x + (width / 4)), (int) y, (int) width / 2, (int) height / 2);
}

public Rectangle getBoundsRIGHT() {
    return new Rectangle((int) ((int) x + width - 5), (int) y + 5, (int) 5, (int) height - 10);
}

public Rectangle getBoundsLEFT() {
    return new Rectangle((int) x, (int) y + 5, (int) 5, (int) height - 10);
}

}

public class Handler {

public ArrayList<GameObject> objects = new ArrayList<GameObject>();
public ArrayList<GameObject> bullets = new ArrayList<GameObject>();
public ArrayList<GameObject> collectedItems= new ArrayList<GameObject>();
private GameObject tempObject;

public void tick() {

    for (GameObject objectt : objects) {
        objectt.tick(objects);
    }

    for (GameObject bulletss : bullets) {
        bulletss.tick(bullets);
    }

    for (GameObject element : new ArrayList<GameObject>(bullets)) {
        if (element.isVisible() == false) {

            bullets.remove(element);
        }
    }



}

public void render(Graphics g) {

    for (GameObject o : objects) {
        o.render(g);

    }

    for (int i = 0; i < bullets.size(); i++) {
        tempObject = bullets.get(i);
        tempObject.render(g);

        /**
         * andere variante for(GameObject o: object){ o.render(g);
         */

    }

}

public void addObject(GameObject object) {

    this.objects.add(object);
}

public void addBullet(GameObject object) {

    this.bullets.add(object);
}

public void removeObject(GameObject object) {
    this.objects.remove(object);
}

public void removeAll() {
    this.objects.clear();
}

public int sizeOfObject(ObjectID id) {
    int count = 0;
    for (GameObject object : objects) {
        if (object.getID().equals(id)) {
            count++;
        }
    }
    return count;
}

// public void createLevel() {
//
// for (int xx = 0; xx < Game.WIDTH + 32; xx += 32)
//
// addObject(new Tree(xx, Game.HEIGHT - 32, ObjectID.Tree));
// for (int yy = 0; yy < Game.WIDTH + 32; yy += 32)
// addObject(new Tree(Game.HEIGHT + 166, yy, ObjectID.Tree));
// for (int zz = 0; zz < Game.WIDTH + 32; zz += 32)
// addObject(new Tree(Game.HEIGHT - 609, zz, ObjectID.Tree));
// for (int ww = 192; ww < Game.WIDTH - 256; ww += 32)
// addObject(new Tree(ww, Game.HEIGHT - 192, ObjectID.Tree));
//
// }

/*
 * for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
 * String value = iterator.next(); if (value.length() > 5) { iterator.remove();
 * } }
 */

}

Exception in thread "Thread-2" 
java.util.ConcurrentModificationException
    at 
java.util.ArrayList$Itr.checkForComodification(Unknown  Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
    at window.Handler.tick(Handler.java:19)
    at window.Game.tick(Game.java:96)
    at window.Game.run(Game.java:76)
    at java.lang.Thread.run(Unknown Source)
Tags : java


Answers 1


Don't modify you structures as you are iterating over them.

Instead collect the planned modifications into a collection and apply them afterwards.

So in addObject you instead add to a secondary objectToAdd list and then do a this.objects.addAll(objectToAdd);objectToAdd.clear(); at the end of tick. You do the same with every other method that would change the datastructure.

ratchet freak
ratchet freak
February 13, 2018 11:10 AM

Related Questions


Java Game How to find the x and y for a tile?

Updated March 26, 2015 14:04 PM


Java game check if any of the booleans are true?

Updated March 27, 2015 13:04 PM

New to game programming

Updated April 07, 2015 20:05 PM

How to build an AIManager class

Updated April 10, 2015 19:05 PM


Cache file /home/queryxchang/public_html/apps/frontend/config/../cache/-q-14-154313-java-concurrentmodificationexception- could not be written