I got this error, when the bullet collides with the enemy.

341, 458	 null
341, 458	 null
341, 458	 null
341, 456	 java.awt.Rectangle[x=340,y=75,width=50,height=50]
341, 456	 java.awt.Rectangle[x=325,y=45,width=50,height=50]
341, 456	 java.awt.Rectangle[x=479,y=15,width=50,height=50]
341, 454	 java.awt.Rectangle[x=340,y=76,width=50,height=50]
<CUT THIS OUT. ABOUT 1000 LINES LIKE THE ONES ABOVE>
305, 352	 java.awt.Rectangle[x=222,y=36,width=50,height=50]
305, 352	 java.awt.Rectangle[x=299,y=6,width=50,height=50]
341, 234	 java.awt.Rectangle[x=340,y=186,width=50,height=50]
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 Draw.run(Draw.java:179)
	at java.lang.Thread.run(Unknown Source)
	at Draw.actionPerformed(Draw.java:139)
	at javax.swing.Timer.fireActionPerformed(Unknown Source)
	at javax.swing.Timer$DoPostEvent.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)

I have never seen this error before. What could cause it. Here is my code that it occurs in

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.Timer;

public class Draw extends JPanel implements ActionListener, KeyListener, Runnable{

	Draw drawing;
	Vector<Enemy> enemy = new Vector<Enemy>();
	Vector<Point> enbullets = new Vector<Point> ();
	Vector<Point> bullets = new Vector<Point> ();
	Image playerBullet, enBullet, playerShip;
	Thread thread = new Thread(this);
	Timer t = new Timer (20, this);
	int px = 225, py = 450;
	Rectangle pRect;
	int shootTimer = 0;
	boolean shootAvailable = true;
	int lives = 5;
	boolean up, down, left, right;
	int enTimer;
	
	
	public void startDraw(Draw d){
		grabFocus();
		drawing = d;
		try{
			playerBullet = ImageIO.read(new File ("Images/playerBullet.png"));
			enBullet = ImageIO.read(new File ("Images/enBullet.png"));
			playerShip = ImageIO.read (new File ("Images/playerShip.png"));
		}catch (IOException e){
			e.printStackTrace();
		}
		addKeyListener (this);
		repaint();
		t.start();
	}
	
	public void createEnemy(){
		Enemy en = new Enemy ();
		enemy.add (en);
		en.sendDrawing(drawing);
		
	}
	
	public void paintComponent (Graphics g){
		super.paintComponent (g);
		for (Enemy en : enemy){
			g.drawImage(en.enImage, en.enx, en.eny, 50, 50, this);
		}
		g.drawImage(playerShip, px, py, 50, 50, this);
		for (Point b : bullets){
			g.drawImage(playerBullet,b.x + 22, b.y + 10, 5, 10, this);
		}
		for (Point b : enbullets){
			g.drawImage(enBullet,b.x + 22, b.y, 5, 10, this);
		}
		
		
		
	}

	public void enemyShot(int enx, int eny) {
		// TODO Auto-generated method stub
		
		
		
	}


	

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_UP){
			up = true;
		}
		if (key == KeyEvent.VK_DOWN){
			down = true;
		}
		if (key == KeyEvent.VK_LEFT){
			left = true;
		}
		if (key == KeyEvent.VK_RIGHT){
			right = true;
		}
		if (key == KeyEvent.VK_SPACE && shootAvailable == true){
			bullets.add (new Point (px + 10,py + 10));
			shootAvailable = false;
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_UP){
			up = false;
		}
		if (key == KeyEvent.VK_DOWN){
			down = false;
		}
		if (key == KeyEvent.VK_LEFT){
			left = false;
		}
		if (key == KeyEvent.VK_RIGHT){
			right = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		shootTimer += 20;
		enTimer += 20;
		if (shootTimer % 500 == 0){
			shootAvailable = true;
			shootTimer = 0;
		}
		if (enTimer == 600){
			Enemy en = new Enemy ();
			enemy.add(en);
			enTimer = 0;
		}
		thread.run();
		
	}
	
	// collision detection
	
	public void run() {
		// TODO Auto-generated method stub
		if (up == true){
			py -= 2;
		}
		if (down == true){
			py += 2;
		}
		if (left == true){
			px -= 2;
		}
		if (right == true){
			px += 2;
		}
		pRect = new Rectangle (px,py,50,50);
		repaint();
		for (Enemy en : enemy){
			en.eny += 1;
		}
		
		
		for (Point b : bullets){
			b.y -= 2;
		}
		for (Point enb : enbullets){
			enb.y += 2;
		}
		
		for (Point p : enbullets){
			if (pRect.contains (p.x+5, p.y+10)){
				lives -= 1;
			}
		}
		for (Point p : bullets){
			for (Enemy enm: enemy){
				System.out.println (p.x + ", " + p.y + "\t " + enm.enRect);
				if (enm.getRect().contains (p.x + 5, p.y)){
					enemy.remove(enm);
				}
			}
		}
		
		
		
	}
	
	
	
	
	
	
}

Thanks for any help

Edited 5 Years Ago by sirlink99: n/a

I fixed the first error, now it happens when I try to remove the bullet when it hits the enemy.

Here is my new code

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.Timer;

public class Draw extends JPanel implements ActionListener, KeyListener, Runnable{

	Draw drawing;
	Vector<Enemy> enemy = new Vector<Enemy>();
	Vector<Point> enbullets = new Vector<Point> ();
	Vector<Point> bullets = new Vector<Point> ();
	Image playerBullet, enBullet, playerShip;
	Thread thread = new Thread(this);
	Timer t = new Timer (20, this);
	int px = 225, py = 450;
	Rectangle pRect;
	int shootTimer = 0;
	boolean shootAvailable = true;
	int lives = 5;
	boolean up, down, left, right;
	int enTimer;
	
	
	public void startDraw(Draw d){
		grabFocus();
		drawing = d;
		try{
			playerBullet = ImageIO.read(new File ("Images/playerBullet.png"));
			enBullet = ImageIO.read(new File ("Images/enBullet.png"));
			playerShip = ImageIO.read (new File ("Images/playerShip.png"));
		}catch (IOException e){
			e.printStackTrace();
		}
		addKeyListener (this);
		repaint();
		t.start();
	}
	
	public void createEnemy(){
		Enemy en = new Enemy ();
		enemy.add (en);
		en.sendDrawing(drawing);
		
	}
	
	public void paintComponent (Graphics g){
		super.paintComponent (g);
		for (Enemy en : enemy){
			g.drawImage(en.enImage, en.enx, en.eny, 50, 50, this);
		}
		g.drawImage(playerShip, px, py, 50, 50, this);
		for (Point b : bullets){
			g.drawImage(playerBullet,b.x + 22, b.y + 10, 5, 10, this);
		}
		for (Point b : enbullets){
			g.drawImage(enBullet,b.x + 22, b.y, 5, 10, this);
		}
		
		
		
	}

	public void enemyShot(int enx, int eny) {
		// TODO Auto-generated method stub
		
		
		
	}


	

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_UP){
			up = true;
		}
		if (key == KeyEvent.VK_DOWN){
			down = true;
		}
		if (key == KeyEvent.VK_LEFT){
			left = true;
		}
		if (key == KeyEvent.VK_RIGHT){
			right = true;
		}
		if (key == KeyEvent.VK_SPACE && shootAvailable == true){
			bullets.add (new Point (px + 10,py + 10));
			shootAvailable = false;
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		int key = e.getKeyCode();
		if (key == KeyEvent.VK_UP){
			up = false;
		}
		if (key == KeyEvent.VK_DOWN){
			down = false;
		}
		if (key == KeyEvent.VK_LEFT){
			left = false;
		}
		if (key == KeyEvent.VK_RIGHT){
			right = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		shootTimer += 20;
		enTimer += 20;
		if (shootTimer % 500 == 0){
			shootAvailable = true;
			shootTimer = 0;
		}
		if (enTimer == 1000){
			Enemy en = new Enemy ();
			enemy.add(en);
			enTimer = 0;
		}
		thread.run();
		
	}
	
	// collision detection
	
	public void run() {
		// TODO Auto-generated method stub
		if (up == true){
			py -= 2;
		}
		if (down == true){
			py += 2;
		}
		if (left == true){
			px -= 2;
		}
		if (right == true){
			px += 2;
		}
		pRect = new Rectangle (px,py,50,50);
		repaint();
		for (Enemy en : enemy){
			en.eny += 1;
		}
		
		
		for (Point b : bullets){
			b.y -= 2;
		}
		for (Point enb : enbullets){
			enb.y += 2;
		}
		
		for (Point p : enbullets){
			if (pRect.contains (p.x+5, p.y+10)){
				lives -= 1;
			}
		}
		boolean hit = false;
		for (Point p : bullets){
			for (Enemy enm: enemy){
				System.out.println (p.x + ", " + p.y + "\t " + enm.enRect);
				if (enm.getRect().contains (p.x + 5, p.y)){
					enemy.remove(enm);
					hit = true;
					break;
				}
				if (hit ==true){
					bullets.remove(p);
				}
			}
			
		}
		
		
		
	}
	
	
	
	
	
	
}

and my error

67, 394	 java.awt.Rectangle[x=473,y=67,width=50,height=50]
67, 394	 java.awt.Rectangle[x=283,y=17,width=50,height=50]
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 Draw.run(Draw.java:179)
	at java.lang.Thread.run(Unknown Source)
	at Draw.actionPerformed(Draw.java:139)
	at javax.swing.Timer.fireActionPerformed(Unknown Source)
	at javax.swing.Timer$DoPostEvent.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)

Edited 5 Years Ago by sirlink99: n/a

This article has been dead for over six months. Start a new discussion instead.