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

**URL:** <https://discourse.processing.org/t/sometimes-when-the-bullet-touch-the-zombie-i-receive-an-error/40053>\
**Category:** Coding Questions\
**Created:** [December 7, 2022, 2:39pm UTC](https://discourse.processing.org/t/sometimes-when-the-bullet-touch-the-zombie-i-receive-an-error/40053 "2022-12-07T14:39:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![C2014](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@C2014](https://discourse.processing.org/u/C2014)\
**Post date:** [December 7, 2022, 2:39pm UTC](https://discourse.processing.org/t/sometimes-when-the-bullet-touch-the-zombie-i-receive-an-error/40053/1 "2022-12-07T14:39:50Z")

</div>

IndexOutOfBoundsException: Index 1 out of bounds for length 1

This is my code, if someone can help me :

```auto

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

```auto
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

```auto
 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);}
}

```

---

<div class="post-metadata">

**Author:** ![C2014](https://avatars.discourse-cdn.com/v4/letter/c/f1d935/32.png) [@C2014](https://discourse.processing.org/u/C2014)\
**Post date:** [December 7, 2022, 4:46pm UTC](https://discourse.processing.org/t/sometimes-when-the-bullet-touch-the-zombie-i-receive-an-error/40053/2 "2022-12-07T16:46:26Z")

</div>

> [@C2014](#):
>
> ```auto
> //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);
>  
> }}}
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 19, 2022, 9:14pm UTC](https://discourse.processing.org/t/sometimes-when-the-bullet-touch-the-zombie-i-receive-an-error/40053/3 "2022-12-19T21:14:00Z")

</div>

> [@C2014](#):
>
> ` bullets.remove(g);`

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)…

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

```
