Explosion in on impact

I am trying to implement a code where when the bird hits something it will explode but I am having trouble with displaying it. It gives me the error “NullPointerException” at code <explosions[a].display();
explosions[a].evaporate();>

This is the code:

PImage backImage; 
PImage birdImage;
PImage wallImage=loadImage("http://i.imgur.com/4SUsUuc.png");
PImage startImage;
int gameState = 1;
int score = 0;
int[] scores = {0, 0, 0, 0};
int highscore = 0;
int total = 100;
String name = "";
String[] names = {"aaaa", "bbbb", "cccc", "dddd"};
String theName = "";
int c= 0;
Explosion[] explosions = new Explosion[total];

//movement of the bird
//vy is the velocity of the bird
int x = -200, y, vy = 0;

//two arrays with the top and bottom walls
int[] wx = new int[2];
int[] wy = new int[2];

//the screen size and images for the game
public void settings() {
  size(600, 800);   
  startImage=loadImage("StartScreen.png");
  birdImage=loadImage("Webp.net-resizeimage (1).png");
  backImage=loadImage("799251_flappy-bird-charizard-edition-on-scratch_1920x1080_h.png");
} 

void draw () { //runs game

  if (gameState == 0) {
    //this will draw images according to top left corner 
    imageMode(CORNER);
    image(backImage, x, 0);
    image(backImage, x+backImage.width, 0);


    //the backgroung moving and the speed
    x -= 2;
    //velocity falls down
    vy += 1;
    y += vy;

    //the game will run in a loop
    if (x == -1800) {
      x = 0;
    }

    for (int i = 0; i < 2; i++) { //drawing the walls
      imageMode(CENTER);
      image(wallImage, wx[i], wy[i] - (wallImage.height/2+100));
      image(wallImage, wx[i], wy[i] + (wallImage.height/2+100));
      if (wx[i] < 0) {
        wy[i] = (int)random(200, height - 200);
        wx[i] = width;
      }

      // if the wall postion where the bird is, then incress the score by one
      if (wx[i] == width/2) {
        score++;
        highscore = max(score, highscore);
      }
      // it checks if the positin of the bird is off the screen or if it touches the walls and it will kill the bird 
      if ( y > height || y < 0 || (abs(width/2 - wx[i]) < 25 && abs(y-wy[i])>100)) {
        background(0);
        for (int a=0; a<explosions.length; a++) {
          explosions[a].display(); //**THE PROBLEM IS HERE**
          explosions[a].evaporate(); //**THE PROBLEM IS HERE**
          gameState = 1;
        }
        textSize(25);
        text("Your score is " + score, width*0.34, height*0.75);
        text("Enter four letters for your name", width*0.18, height*0.25);
        textSize(50);
        text(name, width*0.4, height*0.5);
      }
      wx[i] -= 4;
    }
    image(birdImage, width/2, y);
    //draws the score
    text("" + score, width/2-15, 700);
    textSize(40);
  } else {
    imageMode(CENTER);
    image(startImage, width/2, height/2);
    text("Highest Score: " +highscore, 50 ,width);
    fill(0, 22, 200);
  }
  //if (gameState ==2) {
  //  background(0);
  //  for (int i=0; i<explosions.length; i++) {
  //    explosions[i].display();
  //    explosions[i].evaporate();
  //  }
  //  textSize(25);
  //  text("Your score is " + score, width*0.34, height*0.75);
  //  text("Enter four letters for your name", width*0.18, height*0.25);
  //  textSize(50);
  //  text(name, width*0.4, height*0.5);
  //}
}

//when clicking the mouse the bird will go up with the velocity of negative
void mousePressed() {
  //when clicking the mouse the bird will go up with the velocity of negative
  vy = -17;
  //click to start the game and position the walls
  if (gameState == 1) {
    wx[0] = 60;
    wy[0] = y = height/2;
    wx[1] = 900;
    wy[1] = 600;

    //bird starts and "x" represents the background positions 
    x = gameState = score = 0;
    //y = height / 2;
  }
}
class Explosion
{
  float posX = 0;
  float posY = 0;
  float c = 255;
  float velocityX = random(-5,5); // when generated, the particle is given a random velocity on the x axis
  float velocityY = random(-5,5); // when generated, the particle is given a random velocity on the y axis
  float shrink = random(10); // when generated, the particles shrink at a random rate
  float exHeight = random(1, 10); // when generated, the ellipses have different radial heights
  float exWidth = random(1, 10); // when generated, the ellipses have different radial widths.


  //constructor gets position in pixels and x and y velocities, and sets the class variables accordingly

  Explosion(float x, float y) {
    posX = x;
    posY = y;
  }
  
  // draws the ellipse of each explosion on the canvas
  void display()
  {
    stroke(c);
    ellipse(posX, posY, exWidth, exHeight);
  }
  // the particles become smaller, and they move out in a random directions defined by the variables.
  void evaporate()
  {
    exWidth =- shrink;
    exHeight =- shrink;
    posX += velocityX;
    posY += velocityY;
  }
}

So you have a explosion array which length is 100, but you never put anything to the array. It’s just an array of NullPointers, that’s why you get and NullPointerException.

One way to sort this out would be to use better suiting datastructure like java.util.ArrayList, which length is actually the same as object stored in it. Or you can use a variable that tells how many explotions have actually been stored to your array and use it as upper limit in the for loop. Or you can check if(explosion[a] == Null) and then not call display and evaporate function. I wouldn’t do the last one, because it’s ugly brute force solution.

I always forget that processing has useful and easy to use array structures. In this case best choice would be ArrayList

Then for loop would simply be

for(Explosion ex: explosions) {
    ex.display();
    ex.evaporate();
    gameState = 1;
}

I tried this one but I get the same error message on the same line “NullPointerException”

Here’s a complete example that doesn’t throw exception. I used Interger, but you can replace it with any class

ArrayList<Integer> inventory;

void setup() {
  inventory = new ArrayList();
  //inventory.add(50);
}

void draw() {
  for(int val: inventory) {
    print(val);
  }
}
ArrayList<Explosion> inventory; //Changed to Explosion

inventory = new ArrayList();

void setup() {
  inventory = new ArrayList();
}

void draw() {
  for(int val: inventory) {
    print(val);
  }
}

I think I wrote it incorrectly but I am getting an error again “cannot convert from element type RetardBird.Explosion to int

Sorry, I am still at the beginning

You need to use right type in for loop too

for(Explosion ex: inventory) {

Thank you for your time SomeOne, It’s still not working but I really appreciate your help