# Trying to write a simple snake game with arrays but I run into an error

**URL:** <https://discourse.processing.org/t/trying-to-write-a-simple-snake-game-with-arrays-but-i-run-into-an-error/19065>\
**Category:** Coding Questions\
**Created:** [March 27, 2020, 1:33am UTC](https://discourse.processing.org/t/trying-to-write-a-simple-snake-game-with-arrays-but-i-run-into-an-error/19065 "2020-03-27T01:33:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![creedin](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@creedin](https://discourse.processing.org/u/creedin)\
**Post date:** [March 27, 2020, 1:33am UTC](https://discourse.processing.org/t/trying-to-write-a-simple-snake-game-with-arrays-but-i-run-into-an-error/19065/1 "2020-03-27T01:33:18Z")

</div>

```auto
float[] xpos;
float[] ypos;
void setup() {
  size(500,500);
}
void draw() { 
  background(255);
  for (int i = 0; i < xpos.length - 1; i++) {
    xpos[i] = xpos[i + 1]; 
    ypos[i] = ypos[i + 1]; 
  } 
  xpos[xpos.length-1] = mouseX; 
  ypos[ypos.length-1] = mouseY; 
  for (int i = 0; i < xpos.length; i++) { 
    noStroke();  
    fill(255-i*5); 
    ellipse(xpos[i],ypos[i],i,i); 
  }
} 

```

It gives me NullPointerException on line 8

---

<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:** [March 27, 2020, 1:58am UTC](https://discourse.processing.org/t/trying-to-write-a-simple-snake-game-with-arrays-but-i-run-into-an-error/19065/2 "2020-03-27T01:58:38Z")

</div>

Hello,

Declare and create a new array:

```auto
float[] xpos = new float[50];
float[] ypos = new float[50];

```

Now `xpos.length` will equal something!

References:  
[https://processing.org/tutorials/arrays/](https://processing.org/tutorials/arrays/)  
[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)

`:)`

---

<div class="post-metadata">

**Author:** ![creedin](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@creedin](https://discourse.processing.org/u/creedin)\
**Post date:** [March 27, 2020, 8:25pm UTC](https://discourse.processing.org/t/trying-to-write-a-simple-snake-game-with-arrays-but-i-run-into-an-error/19065/3 "2020-03-27T20:25:11Z")

</div>

Ohhhh, silly mistake. I just learned about arrays so I’m not too familiar with them yet. Thanks for the reply, I was really worried that the problem was a bigger issue.
