java - What is the "ConcurrentModificationException"? -
this question has answer here:
this 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 made 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 error on 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 error message:
exception in thread "thread-4" java.util.concurrentmodificationexception @ java.util.linkedlist$listitr.checkforcomodification(unknown source) @ java.util.linkedlist$listitr.next(unknown source) then showed lines errors. have no idea why it's not working. please me. mean lot. :-)
oh, forgot, removebullet method bulletqualities class:
public void removebullet(bullets bullet){ bulletlist.remove(bullet); }
you getting exception because of removing element while iterating list.
for(bullets bullet:bulletlist){ if(bullet.gety() == 0) removebullet(bullet); // removing element during iteration. bullet.tick(); } `
Comments
Post a Comment