Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

ConcurrentModificationException

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm making a little spaceshooter. But now I have a problem with updating the bullets. The actionperformed method is runned every second by a timer.

public void actionPerformed(ActionEvent evt) {
for (Bullet b : bullets) {
int y = b.getY();
if (y > 0) {
b.setLocation(b.getX(), b.getY() - b.getSpeed());
} else {
jlayeredpane.remove(b);
bullets.remove(b);
jlayeredpane.repaint();
}
}
}




then I tried with an iterator:
Iterator<Bullet> itr = bullets.iterator();

public void actionPerformed(ActionEvent evt) {
while (itr.hasNext()) {
Bullet b=itr.next();

int y = b.getY();
if (y > 0) {
b.setLocation(b.getX(), b.getY() - b.getSpeed());
} else {
jlayeredpane.remove(b);
bullets.remove(b);
jlayeredpane.repaint();
}
}
}


then I tried making it synchronized:

ActionListener bulletlistener = new ActionListener() {

public void actionPerformed(ActionEvent evt) {
updateBullets();
}
};
public synchronized void updateBullets() {
for (Bullet b : bullets) {
int y = b.getY();
if (y > 0) {
b.setLocation(b.getX(), b.getY() - b.getSpeed());
} else {
jlayeredpane.remove(b);
bullets.remove(b);
jlayeredpane.repaint();
}
}
}




Can anyone help me? or does someone know a decent link for this problem? (yes I googled myself already:))

tnx

edit: iterator is only good when deleting? It's no use the way I'm using it? or am I wrong?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't delete from the list you are iterating over (bullets). Thats whats generating the exception.
You should make a copy of the original list, and delete the members from the copy, the reassign the copy to the original.
Also never synchronize unless you have no other option.
 
Cunces Corten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i still have exceptions :s

here's my code:

public void actionPerformed(ActionEvent evt) {
ArrayList <Bullet> templist = getBullets();
for (Bullet b : templist) {
int y = b.getY();
if (y > 0) {
b.setLocation(b.getX(), b.getY() - b.getSpeed());
} else {
jlayeredpane.remove(b);
bullets.remove(b);
jlayeredpane.repaint();
}
}
bullets=templist;
}
};


public ArrayList<Bullet> getBullets() {
return bullets;
}


Did I do something wrong?
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from not using Code Tags, just one thing:
Whenever you iterate over any collection using an Iterator (and the for-each loop uses an Iterator in the background), there is just one way to remove elements: using that Iterator:
Not a Swing problem so moving to Java in General.
 
Cunces Corten
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Tnx, Rob. Got it working now
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic