Results 1 to 19 of 19
  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Problem with ArrayList's

    Hello everyone. I am making my first Java game...Asteroids. It's going remarkably well so far, You can fly, shoot, collect powerups, destroy different sized asteroids etc.....But one thing is that it's throwing errors at me when i am able to shoot too quickly it seems.
    I have 2 ArrayLists, one to hold objects that are in the game, and since i can't remove objects from an arraylist that i am looping through, I have one to remove objects from the first array too,I call a method to remove those objects from the arraylists, and then i will call to repaint the screen.
    Java Code:
    public static ArrayList<Asteroid> removeasteroids = new ArrayList<Asteroid>();
    	public static ArrayList<Asteroid> removeasteroidsbuffer = new ArrayList<Asteroid>();
    	public static ArrayList<PlayerShip> removeplayership = new ArrayList<PlayerShip>();
    	public static ArrayList<PowerUp> removepowerups = new ArrayList<PowerUp>();
    	public static ArrayList<Bullet> removebullets = new ArrayList<Bullet>();
    Java Code:
    			generateAsteroids();
    			generatePowerups();
    			getInput();
    			updateMovement();
    			checkCollision();
    			removeObjects();
    			repaint();
    The error is in my repaint() method, and it's an error stating that it can't find a bullet object in the array it is looking in. That means my removeObjects method has been called again, or my repaint() method has been called too quickly because the only time i remove objects from that array is in 'removeObjects()'. Can anyone explain to me how this can happen when i thought the code should be following in order, and not moving on until it has finished?

    Thanks
    Last edited by Teedo; 04-16-2012 at 01:00 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Problem with ArrayList's

    Please post the full text of the error message and the code that is causing the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Problem with ArrayList's

    Java 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)
    Java 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
    Last edited by Teedo; 04-16-2012 at 01:39 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Problem with ArrayList's

    Also make yourself aware of the EDT. Find the tutorial on Concurrency in Swing and go through it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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?
    Java Code:
    Iterator<Bullet> litb = Screen.bullets.iterator();
    			while ( litb.hasNext()) {
    				Bullet b = ????????
    How would i assign 'b' to the current loop bullet?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Problem with ArrayList's

    Read the API doc for the Iterator class. It has a method to get the next item.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.
    Java 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?
    Java 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();
    					}
    				}
    			}
    Java 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 :)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Problem with ArrayList's

    Which remove() method is being called? Has it already been called once for that Iterator?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.

    Java 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.....
    Java 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)
    Java 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();

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default Re: Problem with ArrayList's

    To be sure that remove() is only being called once, add a println just before it to verify.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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....

    Java 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();
    		}
    Java 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)
    Java 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)

  14. #14
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Problem with ArrayList's

    Java 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;	
    					}
    				}
    			}
    Java 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)
    Java 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
    Java 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?
    Last edited by Teedo; 04-21-2012 at 11:34 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Problem with ArrayList's

    Java 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

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default 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);
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,573
    Rep Power
    25

    Default 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?
    Last edited by Norm; 04-21-2012 at 04:12 PM. Reason: added comment re threads
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. ArrayList problem
    By mathidioticz in forum New To Java
    Replies: 9
    Last Post: 02-04-2012, 06:39 PM
  2. ArrayList problem
    By sharingan90 in forum New To Java
    Replies: 5
    Last Post: 12-17-2011, 01:10 PM
  3. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  4. Arraylist problem
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-28-2010, 10:38 AM
  5. ArrayList problem
    By brian.baert in forum New To Java
    Replies: 8
    Last Post: 03-06-2010, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •