Null pointer exception on for loop over ArrayList

hi everyone this is my first posed i have to make a came for school and i have choisen crossy road.
but i get nullpointerexeption and i dont know why

this is my code:

// array s 
ArrayList <Car> carsList;
ArrayList <TreeTrunk> TrunkList;
//ArrayList <Car> cars2;

TreeTrunk [] ArrayTreeTrunk= new TreeTrunk [3]; 
Car [] CarsArr;
Car Auto;
PImage [] imgcars = new PImage [3];
int [] CarYpos = new int [2]; 
int [] TrunkYpos= new int [3];

PImage auto1, auto2, auto3;
int speedCar1, speedCar2, xposCar1=0, xposCar2=0;
int randomCar, randomYposCar, CounterJump, counter; 

Chicken chick;


//Background BG1, BG2;
Car Car1, Car2;
PImage bg1, bg2, bg[] = new PImage [2];
int TreeYpos;
boolean level1, level2;
int level; 
int chickXpos, chickYpos;

void setup()
{
  size(1049, 656);

  bg[0] = loadImage("level1.jpg");
  bg[1] = loadImage("level2.jpg");

  chickXpos=width/2;
  chickYpos=height-50;
  chick =  new Chicken (chickXpos, chickYpos);

  // car back up zo lang random mize niet werkt 
  auto1=loadImage("C-01.png");
  auto2=loadImage("C-02.png"); 
  auto3 =loadImage("C-00.png");

  for ( int i =0; i< imgcars.length; i++)
  {
    imgcars[i] = loadImage("C-0"+i+".png");
  }

  level=0;



  if ( level1==true )
  {
    level =1;  
    if ( level ==1)
    {
      carsList= new ArrayList <Car>();
      CarYpos [0] = height-280;
      CarYpos [1] = height-180;
      randomCar= int ( random( imgcars.length));
      randomYposCar = int ( random ( CarYpos.length));



      //Car Auto = new Car ( auto1, width - auto1.width, height-280);
      Auto = new Car ( width - (imgcars[randomCar].width), CarYpos[randomYposCar], imgcars[randomCar]);

      ArrayTreeTrunk[0]= new TreeTrunk(0, 100, 1);
      ArrayTreeTrunk[1]= new TreeTrunk(10, 150, 1);
      ArrayTreeTrunk[2]= new TreeTrunk(50, 240, 1);
    }
  }

  if (level2==true)
  {
    if (level==2)
    {

      println("01");



      //chickXpos=width/2;
      //chickYpos=height-50;
      carsList= new ArrayList <Car>();


      ArrayTreeTrunk[0]= new TreeTrunk(0, 100, 1);
      ArrayTreeTrunk[1]= new TreeTrunk(10, 150, 1);
      ArrayTreeTrunk[2]= new TreeTrunk(50, 240, 1);
    }
  }
}


void draw()
{

  //reset();
  level();
  //GameOver();
  cars();


  Chick();
}

void cars ()
{
 

 println ("start");
    for ( int i=0; i<carsList.size(); i++)
    {
      //laat cars zien
      carsList.get(i).move();
      carsList.get(i).display();

      // voegt cars toe 
           //carsList.add(Auto);
      if (carsList.get(i).getXposCar()>=width)
      {
        carsList.add(Auto);
      }

      //verwijdert Car

      Car c = carsList.get(i);
      if (c.getXposCar()+c.getWidth()>width)
      {
        carsList.remove(c);
      }
    }
  }

void TreeTrunk()
{
  if (level == 1)
  {
  }

  if (level == 2 )
  {
  }
}

void Chick()
{
  if (level ==1 || level == 2)
  {
    chick.display();
    chick.Bounderie();
    if (keyPressed)
    {
      chick.move();
      chick.jump();
    } else
    {
      chick.keyReleased();
    }
  }
}

void level()
{
  if ( level==0)
  {
    fill(255);
    rect(0, 0, width, height);
    level1=false; 
    level2=false;

    fill(0);
    textSize(50);
    text("press space bar", width/2-200, height/2);
    text("to start the game", width/2-220, height/2+50);
    if (keyPressed)
    {
      if ( key == ' ')
      {
        level=1;
      }
    }
  }

  if (level == 1 )
  {    
    level1=true;
    level2=false;

    image(bg[0], 0, 0, width, height);
    //ArrayBG[0].display();

    Chick();
    //cars();
    fill(0);
    textSize(20);

    text ("jump = spacebar", 10, 20);
    text ("move = arrow keys", 10, 40 );
    if ( counter == 50 )
    {
      cars();
    }
    if (chick.GetyPosChick()==10)
    {
      level=2;

      //chickXpos=width/2;
      //chickYpos=height-50;
    }
  }

  if (level==2)
  {


    level2=true;
    level1=false; 
    image(bg[1], 0, 0, width, height);
    Chick();

    //if (level2==true)
    //{
    //    chickXpos=width/2;
    //    chickYpos=height-50;
    //    //ArrayBG[1].display();
    //    //BG2.display();
    //  }
  }
}

void reset ()
{
  if (key == 'r')
  {
    level =0;
  }
}

void GameOver()
{
  if (chick.getAliveChick()==false )
  {
    textSize(60);
    textMode(CENTER);
    text("GAME OVER", width/2-50, height/2);
    chick.DeadChick();
  }
}

Which line is highlighted when you get the error?

for ( int i=0; i<carsList.size(); i++)

Okay. So then the problem is obviously that carsList has not been created. You do have this line in your code, however:

carsList= new ArrayList <Car>();

But look at where it is placed:

  level=0;

  if ( level1==true )
  {
    level =1;  
    if ( level ==1)
    {
      carsList= new ArrayList <Car>();

So, since level1 defaults to a value of false, the conditions to create this ArrayList are not met before you try to use it.

The way to solve this is to make sure that you have created this ArrayList. That is, create it outside of any conditional blocks. You could do it right at the start of setup():

void setup()
{
  size(1049, 656);
  carsList = new ArrayList();

It is hard to help more without being able to run your code. We are missing several of your classes and all of your images. Maybe consider switching to using P5.js and putting your sketch on OpenProcessing?

2 Likes