Re: Problem with ArrayList's
Please post the full text of the error message and the code that is causing the error.
Re: Problem with ArrayList's
Code:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at invasion.Screen.paint(Screen.java:93)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
for (Bullet b : bullets) {
if (b.getPosX() > this.getWidth() +50|| b.getPosY() > this.getHeight() +50|| b.getPosX() < -50 || b.getPosY() < -50) {
Invasion.removebullets.add(b);
} else {
g2d.rotate(-b.getBearing(), b.getPosX() + b.getGraphic().getWidth()/2, b.getPosY() + b.getGraphic().getHeight()/2);
g2d.drawImage(b.getGraphic(), (int)b.getPosX(), (int)b.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(b.collisionbox01.x, b.collisionbox01.y, b.collisionbox01.width, b.collisionbox01.height);
}
Toolkit.getDefaultToolkit().sync();
g2d.setTransform(originalTransform);
}
Toolkit.getDefaultToolkit().sync();
}
The error is when looping through bullets here....But the only time I remove objects from the 'bullets' array is in the method 'removeBullets'...I add them to 'removebullets' first, so i can continue looping through the bullets array
Re: Problem with ArrayList's
Use an Iterator returned by your bullets array rather than the short-form of for loop.
The Iterator has a remove method that will remove the latest selected entry.
That'll save you storing these in a separate list and relying on a later (or in this case apparently concurrent) call.
Re: Problem with ArrayList's
Also make yourself aware of the EDT. Find the tutorial on Concurrency in Swing and go through it.
db
Re: Problem with ArrayList's
Thanks guys, I'm working on the iterator but not sure how to assign the current object looping to a bullet variable?
Code:
Iterator<Bullet> litb = Screen.bullets.iterator();
while ( litb.hasNext()) {
Bullet b = ????????
How would i assign 'b' to the current loop bullet?
Re: Problem with ArrayList's
Read the API doc for the Iterator class. It has a method to get the next item.
Re: Problem with ArrayList's
Ok I've had more of a play around and have come up with this......
The following code does NOT detect collisions....so I tried a for loop.
Code:
Iterator<Asteroid> lita = Screen.asteroids.iterator();
Iterator<Bullet> litb = Screen.bullets.iterator();
while (lita.hasNext()) {
Asteroid a = lita.next();
while ( litb.hasNext()) {
Bullet b = litb.next();
if (b.collisionbox01.intersects(a.collisionbox01)) {
if (isPixelCollide(a.getPosX(),a.getPosY(), a.getGraphic(), b.getPosX(), b.getPosY(), b.getGraphic())) {
System.out.println("collision!");
a.setHealth(a.getHealth()-1);
if (a.getHealth() <= 0) {
switch (a.getSize()) {
case 2:
if (dir == 0) {
Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx, -ran_dy, ran_rotationspd, 1);
} else {
Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx, ran_dy, ran_rotationspd, 1);
}
if (dir2 == 0) {
Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx2, ran_dy2, ran_rotationspd2, 1);
} else {
Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx2, -ran_dy2, ran_rotationspd2, 1);
}
break;
case 3:
Screen.addAsteroids(a.getPosX(),a.getPosY(), -a.getDeltaX(), -a.getDeltaY(), ran_rotationspd, 2);
Screen.addAsteroids(a.getPosX(),a.getPosY(), a.getDeltaX(), a.getDeltaY(), ran_rotationspd, 2);
break;
default:
break;
}
lita.remove();
}
litb.remove();
}
}
}
The following code DOES detect colllisions, But it also throws the same exception as before when i try to lita.remove()....Maybe I am using them wrong?
Code:
for (Iterator<Asteroid> lita = Screen.asteroids.iterator(); lita.hasNext();) {
Asteroid a = lita.next();
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
Bullet b = litb.next();
if (b.collisionbox01.intersects(a.collisionbox01)) {
if (isPixelCollide(a.getPosX(),a.getPosY(), a.getGraphic(), b.getPosX(), b.getPosY(), b.getGraphic())) {
System.out.println("collision!");
a.setHealth(a.getHealth()-1);
if (a.getHealth() <= 0) {
switch (a.getSize()) {
case 2:
if (dir == 0) {
Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx, -ran_dy, ran_rotationspd, 1);
} else {
Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx, ran_dy, ran_rotationspd, 1);
}
if (dir2 == 0) {
Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx2, ran_dy2, ran_rotationspd2, 1);
} else {
Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx2, -ran_dy2, ran_rotationspd2, 1);
}
break;
case 3:
Screen.addAsteroids(a.getPosX(),a.getPosY(), -a.getDeltaX(), -a.getDeltaY(), ran_rotationspd, 2);
Screen.addAsteroids(a.getPosX(),a.getPosY(), a.getDeltaX(), a.getDeltaY(), ran_rotationspd, 2);
break;
default:
break;
}
lita.remove();
}
litb.remove();
}
}
}
Code:
collision!
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at invasion.Invasion.checkCollision(Invasion.java:276)
at invasion.Invasion.run(Invasion.java:98)
at invasion.Invasion.main(Invasion.java:82)
I'm sorry I haven't had a chance to check out EDT properly yet...But I will get on to that this weekend. From my quick look at EDT, It looks like a thread that runs the painting and event/keyboard/mouse events. The arrays are not being modified from within paint or the keyboard/mouse events.
Thanks :)
Re: Problem with ArrayList's
Which remove() method is being called? Has it already been called once for that Iterator?
Re: Problem with ArrayList's
The lita.remove(); on line 31 is being called and giving the error. No it should only have been called once.
Ok if i remove all of the Screen.AddAsteroids (Since that adds to the asteroids arraylist.....) then it gets past that....Most of the time, It still seems to cause the error sometimes. Now I get errors in the paint method which loops through the bullets and asteroids the exact same way. I'm guessing this happens when the bullet/asteroid the loop is currently working on, gets deleted via another thread....but i'm pretty lost right now.
Code:
for (Iterator<Asteroid> lita = Screen.asteroids.iterator(); lita.hasNext();) {
Asteroid a = lita.next();
g2d.rotate(-a.getBearing(), a.getPosX() + a.getGraphic().getWidth()/2, a.getPosY() + a.getGraphic().getHeight()/2);
g2d.drawImage(a.getGraphic(), (int)a.getPosX(), (int)a.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(a.collisionbox01.x, a.collisionbox01.y, a.collisionbox01.width, a.collisionbox01.height);
}
g2d.setTransform(originalTransform);
Toolkit.getDefaultToolkit().sync();
}
// Display the bullets and remove bullets off screen
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
Bullet b = litb.next();
if (b.getPosX() > this.getWidth() +50|| b.getPosY() > this.getHeight() +50|| b.getPosX() < -50 || b.getPosY() < -50) {
//bullets.remove(b);
} else {
g2d.rotate(-b.getBearing(), b.getPosX() + b.getGraphic().getWidth()/2, b.getPosY() + b.getGraphic().getHeight()/2);
g2d.drawImage(b.getGraphic(), (int)b.getPosX(), (int)b.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(b.collisionbox01.x, b.collisionbox01.y, b.collisionbox01.width, b.collisionbox01.height);
}
Toolkit.getDefaultToolkit().sync();
g2d.setTransform(originalTransform);
}
Toolkit.getDefaultToolkit().sync();
}
I am also getting this error now.....
Code:
Exception in thread "main" java.lang.IllegalStateException
at java.util.AbstractList$Itr.remove(Unknown Source)
at invasion.Invasion.checkCollision(Invasion.java:274)
at invasion.Invasion.run(Invasion.java:98)
at invasion.Invasion.main(Invasion.java:82)
Code:
for (Iterator<Asteroid> lita = Screen.asteroids.iterator(); lita.hasNext();) {
Asteroid a = lita.next();
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
Bullet b = litb.next();
if (b.collisionbox01.intersects(a.collisionbox01)) {
if (isPixelCollide(a.getPosX(),a.getPosY(), a.getGraphic(), b.getPosX(), b.getPosY(), b.getGraphic())) {
System.out.println("collision!");
a.setHealth(a.getHealth()-1);
if (a.getHealth() <= 0) {
switch (a.getSize()) {
case 2:
if (dir == 0) {
//Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx, -ran_dy, ran_rotationspd, 1);
} else {
//Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx, ran_dy, ran_rotationspd, 1);
}
if (dir2 == 0) {
//Screen.addAsteroids(a.getPosX(),a.getPosY(), ran_dx2, ran_dy2, ran_rotationspd2, 1);
} else {
//Screen.addAsteroids(a.getPosX(),a.getPosY(), -ran_dx2, -ran_dy2, ran_rotationspd2, 1);
}
break;
case 3:
//Screen.addAsteroids(a.getPosX(),a.getPosY(), -a.getDeltaX(), -a.getDeltaY(), ran_rotationspd, 2);
//Screen.addAsteroids(a.getPosX(),a.getPosY(), a.getDeltaX(), a.getDeltaY(), ran_rotationspd, 2);
break;
default:
break;
}
lita.remove(); // THIS LINE CAUSING ERROR
}
litb.remove();
}
}
}
Which again is with the lita.remove();
Re: Problem with ArrayList's
To be sure that remove() is only being called once, add a println just before it to verify.
Re: Problem with ArrayList's
1. You can't add things to the array the iterator is working on without causing that exception. This is why, presumably, commenting out addAsteroids() calls helps.
2. You can't remove() twice (which is what Norm is saying you need to check). Indeed, why are you continuing to check bullets against an asteroid you've already deleted? Just break from that inner loop right after removing it.
Re: Problem with ArrayList's
Remove is only being called once after i checked with a println.
I'm now adding the asteroids to another list and adding them to the asteroid list later on, And thats working fine. Now the only problem still is the bullets and asteroids in the paint method....I am so lost....
Code:
//ArrayList<Asteroid> removeasteroids = new ArrayList<Asteroid>();
for (Iterator<Asteroid> lita = Screen.asteroids.iterator(); lita.hasNext();) {
Asteroid a = lita.next(); // THIS LINE CAUSING ERROR
g2d.rotate(-a.getBearing(), a.getPosX() + a.getGraphic().getWidth()/2, a.getPosY() + a.getGraphic().getHeight()/2);
g2d.drawImage(a.getGraphic(), (int)a.getPosX(), (int)a.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(a.collisionbox01.x, a.collisionbox01.y, a.collisionbox01.width, a.collisionbox01.height);
}
g2d.setTransform(originalTransform);
Toolkit.getDefaultToolkit().sync();
}
// Display the bullets and remove bullets off screen
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
System.out.println("displaying bullet");
Bullet b = litb.next(); // THIS LINE CAUSING ERROR
System.out.println("displayed bullet");
if (b.getPosX() > this.getWidth() +50|| b.getPosY() > this.getHeight() +50|| b.getPosX() < -50 || b.getPosY() < -50) {
System.out.println("removing bullet");
litb.remove();
} else {
g2d.rotate(-b.getBearing(), b.getPosX() + b.getGraphic().getWidth()/2, b.getPosY() + b.getGraphic().getHeight()/2);
g2d.drawImage(b.getGraphic(), (int)b.getPosX(), (int)b.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(b.collisionbox01.x, b.collisionbox01.y, b.collisionbox01.width, b.collisionbox01.height);
}
Toolkit.getDefaultToolkit().sync();
g2d.setTransform(originalTransform);
}
Toolkit.getDefaultToolkit().sync();
}
Code:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at invasion.Screen.paint(Screen.java:99)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at invasion.Screen.paint(Screen.java:127)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Re: Problem with ArrayList's
Code:
int aaa = 0;
int bbb = 0;
for (Iterator<Asteroid> lita = Screen.asteroids.iterator(); lita.hasNext();) {
Asteroid a = lita.next();
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
Bullet b = litb.next();
if (b.collisionbox01.intersects(a.collisionbox01)) {
if (isPixelCollide(a.getPosX(),a.getPosY(), a.getGraphic(), b.getPosX(), b.getPosY(), b.getGraphic())) {
System.out.println("collision!");
a.setHealth(a.getHealth()-1);
if (a.getHealth() <= 0) {
switch (a.getSize()) {
case 2:
if (dir == 0) {
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), -ran_dx, -ran_dy, ran_rotationspd, 1);
} else {
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), ran_dx, ran_dy, ran_rotationspd, 1);
}
if (dir2 == 0) {
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), ran_dx2, ran_dy2, ran_rotationspd2, 1);
} else {
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), -ran_dx2, -ran_dy2, ran_rotationspd2, 1);
}
break;
case 3:
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), -a.getDeltaX(), -a.getDeltaY(), ran_rotationspd, 2);
Screen.addAsteroidsBuffer(a.getPosX(),a.getPosY(), a.getDeltaX(), a.getDeltaY(), ran_rotationspd, 2);
break;
default:
break;
}
aaa+=1;
System.out.println("Removed asteroid " + aaa);
lita.remove();
//break;
}
aaa = 0;
bbb+=1;
System.out.println("Removed bullet " +bbb);
litb.remove();
bbb=0;
break;
}
}
}
Code:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at invasion.Screen.paint(Screen.java:126)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
//System.out.println("displaying bullet");
Bullet b = litb.next(); // LINE 126
//System.out.println("displayed bullet");
if (b.getPosX() > this.getWidth() +50|| b.getPosY() > this.getHeight() +50|| b.getPosX() < -50 || b.getPosY() < -50) {
//System.out.println("removing bullet");
litb.remove();
} else {
g2d.rotate(-b.getBearing(), b.getPosX() + b.getGraphic().getWidth()/2, b.getPosY() + b.getGraphic().getHeight()/2);
g2d.drawImage(b.getGraphic(), (int)b.getPosX(), (int)b.getPosY(), null);
if (Invasion.Debug) {
g2d.drawRect(b.collisionbox01.x, b.collisionbox01.y, b.collisionbox01.width, b.collisionbox01.height);
}
Toolkit.getDefaultToolkit().sync();
g2d.setTransform(originalTransform);
}
Toolkit.getDefaultToolkit().sync();
}
I am guessing some somewhere between these lines
Code:
for (Iterator<Bullet> litb = Screen.bullets.iterator(); litb.hasNext();) {
//System.out.println("displaying bullet");
Bullet b = litb.next(); // LINE 126
that the next bullet is being removed....but i have no idea how or where, And I don't think it's the EDT..but correct me if i'm wrong?
Re: Problem with ArrayList's
Where is this System.out.println("removing bullet"); or any of the other println calls printed out? I don't see it in what you have posted?
Re: Problem with ArrayList's
Code:
displaying bullet
displayed bullet
collision!
Removed asteroid 1
Removed bullet 1
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at invasion.Screen.paint(Screen.java:126)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
collision!
Removed bullet 1
collision!
Removed bullet 1
displaying bullet
displayed bullet
displaying bullet
displayed bullet
displaying bullet
displayed bullet
Re: Problem with ArrayList's
You should include some information about the bullet objects when you displaying the message about them. The print outs do NOT identify which bullet you are looking at. One way it to add a toString() method to the bullet class that will give you a unique String for each bullet. Then when you print them, that String will ID which one you are working with.
System.out.println("displayed bullet=" + b);
Re: Problem with ArrayList's
I've added an asteroid counter to print how many asteroids are in the game, And when the counter reaches about 250 it starts the errors for Asteroid a = lita.next();
It's not a big deal because I don't need 250 asteroids in the game at once....But what would be causing that?
Re: Problem with ArrayList's
What does your debug print out look like now with the toString() method providing a unique id for each item gotten from the list?
Is there more than one thread that can modify the arraylist at the same time?