• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ConcurrentModificationException

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Tnx, Rob. Got it working now
 
Grow your own food... or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic