Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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: 21866
105
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
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!