# NullPointerException with arrays help

**URL:** <https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543>\
**Category:** Beginners\
**Created:** [December 27, 2020, 10:23pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543 "2020-12-27T22:23:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ironbatman1](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@Ironbatman1](https://discourse.processing.org/u/Ironbatman1)\
**Post date:** [December 27, 2020, 10:23pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543/1 "2020-12-27T22:23:59Z")

</div>

Hey so it’s an array of Test (custom object/class) called tests and I’m trying to do clearing with all objects in the array hence the for loop. so I set them and when I try to use it breaks and gives a null pointer at tests[i].Clear();

```auto

Test[] tests = new Test[3];

void setup(){
  background(56);
  size(600, 600);
  
  for(int i = 0; i > tests.length; i++){
    if(tests[i] == null){
      tests[i] = new Test(300, 300, 1);
    }
  }
  
  System.out.print(tests);

}

void draw(){
  
  for(int i = 0; i < tests.length; i++){
    tests[i].Clear();
  }
  
  background(51);
  
  for(int i = 0; i < tests.length; i++){
    Test t = tests[i];
    //t.Update();
    //t.Display();
  }
  
}

class Test {
  
  int x, y, speed;
  
  Test(int xpos, int ypos, int movespeed){
    
    x = xpos;
    y = ypos;
    speed = movespeed;
    
  }
  
  void Display(){
    
    pushStyle();
    rectMode(CENTER);
    fill(100);
    rect(x, y, 25, 25);
    popStyle();
  
  }
  
  void Update(){
  
    x = x + speed;
    if(x > width){
        x = 0;
    }
  
  }
  
  void Clear(){
    
    clear();
  
  }

}

```

---

<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 27, 2020, 10:47pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543/2 "2020-12-27T22:47:02Z")

</div>

> [@Ironbatman1](#):
>
> `for(int i = 0; i > tests.length; i++){`

This is in setup ()

it needs to be \<

not \>

---

<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 27, 2020, 10:49pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543/3 "2020-12-27T22:49:12Z")

</div>

> [@Ironbatman1](#):
>
> clearing with all objects in the array hence the for loop.

clear clears the canvas I think

You do this with background already anyway

But hey, welcome to the forum!!!

---

<div class="post-metadata">

**Author:** ![Ironbatman1](https://avatars.discourse-cdn.com/v4/letter/i/e47c2d/32.png) [@Ironbatman1](https://discourse.processing.org/u/Ironbatman1)\
**Post date:** [December 27, 2020, 10:57pm UTC](https://discourse.processing.org/t/nullpointerexception-with-arrays-help/26543/4 "2020-12-27T22:57:59Z")

</div>

Ty!!! And oh my gosh how did i not see that !!! Well thank you very much!!!
