Why am I getting a NullPointerException

Hello everyone, this is my first ever personal processing project. I am trying to create a game where you send the colored boxes to their particular location. For example, the red box should go to the red position and the green box should go to the green position. Unfortunately, I am stuck. I aim to have 100 boxes which appear one after another as soon as the previous box reaches its location. I am getting a NullPointerException, I tried to find where, but sadly I couldn’t find anything. It is probably a stupid mistake I made somewhere in the code. Your help will be much appreciated.

Thanks in advance.

Box [] Boxes = new Box [2];    

void setup()
{
  size(800, 600); 
  for(int i = 0; i < 2; i++)
  {
    Boxes = new Box[i]; 
    
  }
}

void draw()
{
  background(80); 
  //RED LINE
  noStroke(); 
  fill(#E52323); 
  rect(40, 0, 10, height*2); 
  
  //GREEN LINE
  noStroke(); 
  fill(#52E523); 
  rect(width - 40, 0, 10, height*2); 
  
   for(int i = 0; i < 2; i++)
   {
     Boxes[i].display(); 
     Boxes[i].update(); 
   }
  
}

void keyPressed()
{
  if(key == CODED)
  {
    if(key == LEFT)
    {
    }
    
    else if(key == RIGHT)
    {
    }
  }
  
}


//CLASS

class Box
{
  float len; 
  float x; 
  float y; 
  float xSpeed; 
  float ySpeed; 
  float size = random(10, 20); 
  float comingTowardsYou = random(0.1, 1); 
  
  Box()
  {
    x = random(width/2 + 1, width/2 - 1);
    y = random(height/2 + 1, height/2 - 1);  
    xSpeed = 0; 
    ySpeed = 0; 
  }
  
  void display()
  {
    rectMode(CENTER); 
    noStroke(); 
    fill(#E52323); 
    rect(x, y, size, size); 
  }
  
  void update()
  {
    size = size + comingTowardsYou; 
  }
  
}
1 Like

it’s in setup()

look at this line in the for loop

Boxes = new Box[i];

2 Likes

also please hit ctrl-t in processing prior to copy and paste

Thanks. I just figured it out.
Feel free to give me some ideas for my first project.

1 Like

Sorry, I am very new to this website. I will use it next time.

1 Like