NullPointerException with arrays help

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();


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();
  
  }

}

This is in setup ()

it needs to be <

not >

1 Like

clear clears the canvas I think

You do this with background already anyway

But hey, welcome to the forum!!!

1 Like

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

1 Like