# Why am I getting a NullPointerException

**URL:** <https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932>\
**Category:** Beginners\
**Created:** [April 3, 2019, 6:43pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932 "2019-04-03T18:43:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasanfarhan33](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@hasanfarhan33](https://discourse.processing.org/u/hasanfarhan33)\
**Post date:** [April 3, 2019, 6:43pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932/1 "2019-04-03T18:43:02Z")

</div>

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.

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

```

---

<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:** [April 3, 2019, 6:48pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932/2 "2019-04-03T18:48:12Z")

</div>

it’s in setup()

look at this line in the for loop

Boxes = new Box[i];

---

<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:** [April 3, 2019, 6:50pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932/3 "2019-04-03T18:50:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hasanfarhan33](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@hasanfarhan33](https://discourse.processing.org/u/hasanfarhan33)\
**Post date:** [April 3, 2019, 7:00pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932/4 "2019-04-03T19:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hasanfarhan33](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@hasanfarhan33](https://discourse.processing.org/u/hasanfarhan33)\
**Post date:** [April 3, 2019, 7:01pm UTC](https://discourse.processing.org/t/why-am-i-getting-a-nullpointerexception/9932/5 "2019-04-03T19:01:33Z")

</div>

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