Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrentModificationException when removing bullet from game
    text
    copied!<p>I have to create Space Invaders for my programming course in college which would be fine but I have a problem with removing a bullet from the game.</p> <p>I'm storing all my enemies and bullets in separate ArrayLists.</p> <p>This is my main class.</p> <pre><code>package uk.ac.wnc.pot13000765.space_invaders; import java.util.ArrayList; import org.lwjgl.LWJGLException; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; import org.lwjgl.opengl.GL11; public class SpaceInvaders { private ArrayList&lt;Enemy&gt; enemies = new ArrayList&lt;Enemy&gt;(); private static ArrayList&lt;Bullet&gt; bullets = new ArrayList&lt;Bullet&gt;(); private Player player; public SpaceInvaders() { //Setup and create the OpenGL Context + Window try { Display.setDisplayMode(new DisplayMode(800, 600)); Display.setTitle("Unit 14 Space Invaders in Java by Liam Potter - POT13000765"); Display.create(); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, 800, 0, 600, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glClearColor(0f, 0f, 0f, 1f); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); } catch(LWJGLException e) { System.err.print(e); System.exit(1); } Time.getDelta(); Time.setLastFps(Time.getTime()); //Setup player = new Player(40, 40, 50, 50); //Enemies enemies.add(new Enemy(40, 540, 25, 25)); while(!Display.isCloseRequested()) { gameLoop(Time.getDelta()); } Display.destroy(); } private void gameLoop(int delta) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); //Update player.update(delta); if(enemies.size() &gt; 0) for(Enemy e: enemies) e.update(delta); if(bullets.size() &gt; 0) for(Bullet b: bullets) b.update(delta); //Render player.draw(); if(enemies.size() &gt; 0) for(Enemy e: enemies) e.draw(); if(bullets.size() &gt; 0) for(Bullet b: bullets) b.draw(); Display.sync(60); Display.update(); Time.updateFps(); } public int getWindowWidth() { return Display.getWidth(); } public int getWindowHeight() { return Display.getHeight(); } public static void spawnBullet(Bullet b) { bullets.add(b); } public static void destroyBullet(Bullet b) { bullets.remove(b); } public static void main(String[] args) { new SpaceInvaders(); } } </code></pre> <p>And this is my Bullet class:</p> <pre><code>package uk.ac.wnc.pot13000765.space_invaders; import org.lwjgl.opengl.GL11; public class Bullet extends Entity { public Bullet(int x, int y, int width, int height) { super(x, y, width, height); } @Override public void update(int delta) { if(!(getY() &gt; 600)) setY(getY() + 1); else dispose(); System.out.println(getY()); } @Override public void draw() { GL11.glColor3f(1.0f, 0.5f, 0.5f); GL11.glBegin(GL11.GL_QUADS); GL11.glVertex2i(getX(), getY()); GL11.glVertex2i(getX() + getWidth(), getY()); GL11.glVertex2i(getX() + getWidth(), getY() + getHeight()); GL11.glVertex2i(getX(), getY() + getHeight()); GL11.glEnd(); } @Override public void dispose() { SpaceInvaders.destroyBullet(this); } } </code></pre> <p>This is the exact error I am getting:</p> <pre><code>Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at uk.ac.wnc.pot13000765.space_invaders.SpaceInvaders.gameLoop(SpaceInvaders.java:62) at uk.ac.wnc.pot13000765.space_invaders.SpaceInvaders.&lt;init&gt;(SpaceInvaders.java:50) at uk.ac.wnc.pot13000765.space_invaders.SpaceInvaders.main(SpaceInvaders.java:91) </code></pre> <p>I understand that I can use ListIterators to fix this but I can't figure out how to implement them in my main class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload