-2

This is my Bullets class

package mainPackage;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Bullets {

    private double x;
    private double y;
    private BufferedImage bulletImage;
    Bullets(double x, double y){
        this.x = x;
        this.y = y;
        ImageLoader loader = new ImageLoader();
        SpriteSheet ss = new SpriteSheet(loader.loadImage("/Pics/TheSpriteSheet.png"));
        bulletImage = ss.grabImage(2, 1, 32, 32);
    }

    public void render(Graphics g){
        g.drawImage(bulletImage, (int)x, (int)y, null);
    }
    public void tick(){
        y--;
    }
    public double getX(){
        return x;
    }
    public double getY(){
        return y;
    }
}

Then I made my BulletQualities class

package mainPackage;

import java.awt.Graphics;
import java.util.LinkedList;

public class BulletQualities {
    Bullets b;
    private LinkedList<Bullets> bulletList = new LinkedList<Bullets>();

    public void addBullet(Bullets b){
        bulletList.add(b);
    }
    public void tick(){

I get an error on this next line

        for(Bullets bullet:bulletList){
                if(bullet.getY() == 0)
                removeBullet(bullet);
            bullet.tick();
        }
    }
    public void render(Graphics g){
        for(int x = 0;x < bulletList.size(); x++){
            bulletList.get(x).render(g);
        }
    }
    public void removeBullet(Bullets bullet){
        bulletList.remove(bullet);
    }
}

This is my error message:

Exception in thread "Thread-4" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    at java.util.LinkedList$ListItr.next(Unknown Source)

Then it just showed the lines with errors. I have no idea why It's not working. Please help me. It would mean a lot. :-)

Oh, I almost forgot, this is my removeBullet method from my bulletQualities class:

public void removeBullet(Bullets bullet){
    bulletList.remove(bullet);
}
Lt Lobster
  • 41
  • 7
  • 1
    You changed a collection while iterating it. – Benjamin Gruenbaum May 30 '15 at 16:36
  • I have closed your question as a duplicate of [this question](http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing). In the future, please use the search functionality of this site before asking questions that likely have been asked *many* times before. For instance, [this search](http://stackoverflow.com/search?q=%5Bjava%5D+ConcurrentModificationException+ArrayList+remove) will get you many answers that are relevant to your problem. – Hovercraft Full Of Eels May 30 '15 at 16:39

2 Answers2

0

You are getting this Exception because of removing an element while iterating the list.

for(Bullets bullet:bulletList){
                if(bullet.getY() == 0)
                removeBullet(bullet);  // removing an element during iteration.
            bullet.tick();
        }

`

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • Thank you so much! I really do appreciate you taking the time (even if it's only like 2 seconds) to tell me what I did wrong! thanks! – Lt Lobster May 30 '15 at 16:57
0

You are changing the collection when you are iterating to it and hence the exception.

Do note that Java8 has a new removeIf method.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331