Sometimes when the bullet touch the zombie, i receive an error!

IndexOutOfBoundsException: Index 1 out of bounds for length 1

This is my code, if someone can help me :


Enemy[] zombie;
int maxzombie=8;
//Enemy zombie,zombie2,zombie3;
boolean enemyspawn=false;

PImage background,gif,tower3,tower,archer;
int i;
Player player = new Player();
//Bullet arraylist
ArrayList<Bullet> bullets;
ArrayList<Enemy> enemies;
//Vars to regulate shooting speed
boolean canShoot = true;
float canShootCounter;
int g;

//PVector mouse;
void setup()
{
  zombie = new Enemy[maxzombie];
  
  for ( int i = 0;i<maxzombie;i++)
 zombie[i] = new Enemy();
  
   bullets = new ArrayList<Bullet>();
  player = new Player();
  cursor(CROSS);
  size (1280,720);
  frameRate(60);
background=loadImage("back2.png");
gif=loadImage("zombie.gif");
tower3=loadImage("tttower.png");
archer=loadImage("archer.png");
tower=tower3.get(170,100,150,360);


}
void draw()
{
  image(); //image
 
                   //enemy

for ( int i = 0;i<maxzombie;i++) {
    if(enemyspawn==true){
    zombie[i].display();
    zombie[i].update();}}



  
  
  //arrows 
  println(frameRate);
   player.update();
  //since we are adding and removing objects so often, we need to do the forloop backwards preventing any nullPointer errors
  for ( g = bullets.size()-1; g >= 0; g--) {
    //you need a seperate var to get the object from the bullets arraylist then use that variable to call the functions
    Bullet bullet = bullets.get(g);
    bullet.update();}
  
}


 



    
    
  

Bullet

class Bullet {
  
  PVector location;

  float oldPosX, oldPosY, rotation, speed;
  Bullet() {

    location= new PVector(180, 260);

    oldPosX = mouseX;
    oldPosY = mouseY;
    rotation = atan2(oldPosY - location.y, oldPosX - location.x) / PI * 180;

    speed = 10;
  } 
  void update() {

    location.x = location.x + cos(rotation/180*PI)*speed;
    location.y = location.y + sin(rotation/180*PI)*speed;
    rectMode(CORNERS);
    fill (#48371A);
   // rect(location.x-25, location.y, location.x+30, location.y+3);


  
    circle(location.x, location.y,30);

    
    loop();
    //triangle (location.x+20,location.y-5,location.x+40, location.y,location.x+20, location.y+5);
for (i=0;i<maxzombie;i++)
    if (location.x+40 > zombie[i].x+50 && location.x < zombie[i].x+90 && location.y > zombie[i].y && location.y < zombie[i].y+90) {
    bullets.remove(g);
 
  }}}

Enemy class

 class Enemy
{
  float x,y,speed;
    Enemy()
  {x=1300;y=random(575,585);speed=random(0.5,2.5);}
  void update(){
    x-=speed;
  }
  void display(){
      image(gif,x,y,90,90);}
}

it caused by this for instructios, when 1 bullet git 2 enemies at the same moment

that’s dangerous

instead of removing, mark the bullet as dead (new variable in the class that is initially false)

don’t check for bullets that are dead (if(!dead)…)

then in a separate for-loop move backwards through bullets and remove the dead ones. if(dead)…

  for ( g = bullets.size()-1; g >= 0; g--) {