0

i am making a shooting game as a project where the enemy object shoots randomly at the actor. but each time the enemy shoots at random, a java.util.ConcurrentModificationException is thrown. this is the code for shooting randomly

public void enemyAttackStrategy() {

    // Fire at when at around the 1/4, 1/2 and 3/4 in a random direction
    int width = Gui.getInstance().getWidth();
    int firstPoint = width / 4 ;
    int secondPoint = firstPoint * 2;
    int thirdPoint = firstPoint * 3;

    int dist = 2;

    boolean nearFirst  = (getX() - firstPoint)  < 3 && (getX() - firstPoint > 0) ;
    boolean nearSecond = (getX() - secondPoint) < 3 && (getX() - secondPoint > 0) ;
    boolean nearThird  = (getX() - thirdPoint)  < 3 && (getX() - thirdPoint > 0);

    if(nearFirst || nearSecond || nearThird){

      //System.out.println("near!!!!!!!!" + (firstPoint) + " " + (secondPoint) + " " + (thirdPoint));
      Game.getInstance().enemyShot();
    }

and the code that creates the enemybullet

public void enemyShot() {
    Bullet bullet = new Bullet("EnemyBullet", "Bullet.png", 14);
    bullets.add(bullet);
    int minSpeed = -15;
    int xPosition = enemyShip.getX();
    int yPosition = enemyShip.getY();
    int xSpeed = random.nextInt(30) + minSpeed;
    int ySpeed = random.nextInt(30) + minSpeed;

    addToWorld(bullet, xPosition, yPosition, xSpeed, ySpeed, 2);

    //System.out.println("Added Enemy Bullet");
}

this is the for loop its referring me to

public void timer() {


    for (Tame oneTame : tames) {
        tame.timeTick();//} 
       }

and this is the error

java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:886)
at java.util.ArrayList$Itr.next(ArrayList.java:836)
at GameFrameworkJavaFX.Game.timeTick(Game.java:135)
H pingos
  • 1
  • 1
  • 3
    You haven't published all the relevant code, show us the loop that is calling `enemyAttackStrategy()` :) – Nir Alfasi Mar 08 '15 at 21:38
  • the the loop calling the enemyAttackStrategy() is in the enemy class. but the java.util.ConcurrentModificationException is refereing me to a for loop in the game class. – H pingos Mar 08 '15 at 21:43
  • Post [SSCCE](http://sscce.org/). Also take a look at http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing – Pshemo Mar 08 '15 at 21:44
  • Copy/paste stacktrace into the question. Are you using any Collections (Lists, Maps, Sets etc?) – NickJ Mar 08 '15 at 21:49
  • @Hpingos It looks like you're modifying `tames` in `timeTick()`. If so, that would explain it. Can we see the code for `timeTick()`? – Paul Boddington Mar 08 '15 at 21:52

1 Answers1

2

A ConcurrentModificationException can happen if you have two threads going that are both modifying the same code. This is commonly caused if you have a for each loop modifying the contents of an array. See this question for further details about that specific problem. I don't know if that's what's causing the problem, but like @alfasin commented, we can't answer this question without seeing the code that is calling these methods.

Edit: Seeing your newly posted code, it looks like this very likely the case. Again, just check out this question, as this seems to be an error someone else had of exactly the same type.

Community
  • 1
  • 1
Josh Desmond
  • 640
  • 2
  • 10
  • 19
  • 1
    "*A ConcurrentModificationException can happen if you have two threads going that both modifying the same code*" that is one of the cases, as you noted this can be also caused by one thread if it modifies collection which he iterates. "*I don't know if that's what's causing the problem*" in that case you shouldn't be posting it as answer. When you will get 50 reputation points you will be able to leave comments, so before you will get this privilege avoid posting comments as answers. – Pshemo Mar 08 '15 at 21:49
  • 1
    A bit harsh! The answer, while not exactly solving the problem, contains potentially useful information which might help the OP track down the problem. – NickJ Mar 08 '15 at 21:52
  • 1
    I would add that this exception can also happens when your changing the collection you're iterating over (singlethreaded). Plus one. – Nir Alfasi Mar 08 '15 at 22:31