Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!

Cunces Corten

Greenhorn
+ Follow
since Aug 24, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Cunces Corten

Steve Luke wrote:Hi Cunces,

Next time you post code, could you put your code in [code][/code] tags? It makes it a lot more readable. There is a button at the top of the message form you can use for adding the tags for you. You can also go back and edit your post to add them in.

To your question, the cocnurrent modification exception can occur if another thread modifies the list while this thread iterates over it. In your case, you have a different part of code which adds to the list which may happen when the list is being iterated over, so the problem can occur.

To get around this you should use a thread-safe collection which would prevent the ConcurrentModificationException. Take a look at the collections available in the java.util.concurrent package. ConcurrentLinkedQueue might work alright for you, or perhaps CopyOnWriteArrayList.



oké forgot to put in code tags again. Thanks for your post, going to check it out :thumbup:
Hi,
I'm making a game but sometimes I get exceptions.

So in my main class I have a keylistener which starts a thread when the user fires the first time.

1) Main class





So according to me this can't throw java.util.ConcurrentModificationException since I'm using an iterator. When I click on the error it says the error starts here: Bullet b = i.next();

Can someone tell me how this is possible?
btw this exception occurs when I enter spacebar for a long time.. lol


Tnx, Rob. Got it working now
10 years ago
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?
10 years ago
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?
10 years ago
oké thanks for the link. I've got it working now
10 years ago
Hi, I'm having a problem using the JLayeredPane.

you can read my code at http://cunces.pastebin.com/m533144f2

I'm having some problems with adding stuff to my JLayeredPane (defender and level aren't added to the screen).
Can someone tell me why they aren't added?

thanks
10 years ago