NullPointerException returned

why does the following code return NullPointerException?

int fistleft=1;
int fistright=2;
int clapleft=3;
int clapright=4;

int choice1;
int choice2;

int[] correct;

int i=0;


void draw(){
//zet choice1 en choice2 naar een random hand/vuist
choice1= int(random(0.5,4.4));
choice2= int(random(0.5,4.4));

//kijk of hij de goede al heeft 


//als hij klopt, zet dan de bijpassende correct naar die hand/vuist en ga door
if (choice1==choice2){
  correct[i]=choice1;
  i++;
  correct[i]=choice2;
  print(correct[i-1], correct[i]);
  i++;}}

This may help:

https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception

The Processing IDE highlights it for you:

:)

2 Likes

Hi

Your error is her

correct[i]=choice1;
  i++;
  correct[i]=choice2;
  print(correct[i-1], correct[i]);
  i++;}}

The wrong with your array

okay, but what is the problem?

what i got from that is that i have to initialize it.

isn’t this initializing?

https://processing.org/reference/Array.html states:

Arrays are similar to objects, so they must be created with the keyword new .

See Instantiating an Array in Java here:

:)

1 Like

@MoonAlien822

int fistleft=1;
int fistright=2;
int clapleft=3;
int clapright=4;

int choice1;
int choice2;

int[] correct;

int i=0;

void setup(){
  size(640,320);
  correct= new int[1000]; 
}
void draw(){
//zet choice1 en choice2 naar een random hand/vuist
choice1= int(random(0.5,4.4));
choice2= int(random(0.5,4.4));

//kijk of hij de goede al heeft 


//als hij klopt, zet dan de bijpassende correct naar die hand/vuist en ga door
if (choice1==choice2){
  correct[i]=choice1;
  i++;
  correct[i]=choice2;
  print(correct[i-1], correct[i]);
  i++;}}

@MoonAlien822

Now it is necessary to define the i++ because in your example it is counting to infinity something like this

if (correct[i] > what ever) {
      correct[i] = what ever ;
    }