# NullPointerException returned

**URL:** <https://discourse.processing.org/t/nullpointerexception-returned/31818>\
**Category:** Coding Questions\
**Created:** [August 20, 2021, 10:56pm UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818 "2021-08-20T22:56:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [August 20, 2021, 10:56pm UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/1 "2021-08-20T22:56:22Z")

</div>

why does the following code return NullPointerException?

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

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 20, 2021, 11:06pm UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/2 "2021-08-20T23:06:03Z")

</div>

This may help:

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

The Processing IDE highlights it for you:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/37c2bf0be2f19618fba04b39c981ffc548e08913.png)

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [August 20, 2021, 11:53pm UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/3 "2021-08-20T23:53:06Z")

</div>

Hi

Your error is her

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

```

The wrong with your array

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [August 21, 2021, 10:16am UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/4 "2021-08-21T10:16:11Z")

</div>

okay, but what is the problem?

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [August 21, 2021, 10:17am UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/5 "2021-08-21T10:17:53Z")

</div>

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

> [@MoonAlien822](#):
>
> `correct[i]=choice1;`

> [@MoonAlien822](#):
>
> `correct[i]=choice2;`

isn’t this initializing?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 21, 2021, 10:31am UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/6 "2021-08-21T10:31:24Z")

</div>

> [@MoonAlien822](#):
>
> isn’t this initializing?

[Array / Reference / Processing.org](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:

> **[Arrays in Java - GeeksforGeeks](https://www.geeksforgeeks.org/arrays-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [August 21, 2021, 1:45pm UTC](https://discourse.processing.org/t/nullpointerexception-returned/31818/8 "2021-08-21T13:45:06Z")

</div>

@MoonAlien822

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

```
