# Game Error (NullPointerException)

**URL:** <https://discourse.processing.org/t/game-error-nullpointerexception/10856>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [May 2, 2019, 10:12pm UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856 "2019-05-02T22:12:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannieways](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@hannieways](https://discourse.processing.org/u/hannieways)\
**Post date:** [May 2, 2019, 10:12pm UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/1 "2019-05-02T22:12:34Z")

</div>

I’m currently trying to create a game for my computer science class and I’m having a bit of a rocky start, to be honest. What I have below is the basis of the game but I’m running into a problem when I run the code. It displays an error saying “NUllPointerException” and then highlights this line of code: **“image(standingImages[int(frame)], link[0], link[1]);”** I’m hoping to not change any of the code to new things I haven’t done before, as I’m a beginner and I can’t show my teacher code that has things we’ve never done before. If anyone would be willing to help, it would be greatly appreciated! thank you! _(See Code Below)_

```auto
PImage[] rightImages=new PImage[6];

PImage[] leftImages=new PImage[6];

PImage[] standingImages=new PImage[6];// *********************

Float frame=0.0;

//x y for link

int[] link={80, 110};

boolean[] keys=new boolean[128];

boolean moveRight=false;

boolean moveLeft=false;

boolean stand=false;// *********************

void setup() {

  size(300, 300);

  frameRate(60);

  //loading all "standing" images

  for (int i=1; i<standingImages.length; i++) {// ***************************

    standingImages[i]=loadImage("link stand "+(i)+".png");

  }

  //loading all "right" images

  for (int i=1; i<rightImages.length; i++) {

    rightImages[i]=loadImage("linkright"+(i)+".png");

  }

  //loading all "left" images

  for (int i=1; i<leftImages.length; i++) {

    leftImages[i]=loadImage("linkleft"+(i)+".png");

  }

}//end setup

void draw() {

  background(200, 80, 50);

  moveLink();

  if (moveRight) {

    image(rightImages[int(frame)], link[0], link[1]);

  } 

  else if (moveLeft) {

    image(leftImages[int(frame)], link[0], link[1]);

  }

  else if (stand) {// ******************************************

    image(standingImages[int(frame)], link[0], link[1]);

  }

  frame+=0.1;

  if (frame>6) {

    frame=1.0;

  }

}//end draw

void moveLink() {

  if (keys[65]) {//65 is 'a' - move left

    link[0]-=3;

    moveLeft=true;

  } else{

    moveLeft=false;

  }

  if (keys[68]) {//68 is 'd' - move right

    link[0]+=3;

    moveRight=true;

  } else {

    moveRight=false;

  }

  if (keys[65]==false && keys[68]==false){// ******************************

      stand=true;

  }

}//end moveLink

void keyPressed() {

  keys[keyCode]=true;

}

void keyReleased() {

  keys[keyCode]=false;

}

```

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [May 2, 2019, 11:18pm UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/2 "2019-05-02T23:18:20Z")

</div>

are you sure you haven’t added an extra space in this line

```auto
standingImages[i]=loadImage("link stand "+(i)+".png");

```

shouldn’t it be

```auto
standingImages[i]=loadImage("link stand"+(i)+".png");

```

---

<div class="post-metadata">

**Author:** ![hannieways](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@hannieways](https://discourse.processing.org/u/hannieways)\
**Post date:** [May 2, 2019, 11:39pm UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/3 "2019-05-02T23:39:55Z")

</div>

I tried that out but it still comes up with NullPointerException on the same line unfortunately

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [May 3, 2019, 12:27am UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/4 "2019-05-03T00:27:07Z")

</div>

in this part of the code where you load the images you skip loading an image into the first array index.

```auto
for (int i=1; i<standingImages.length; i++) {
    standingImages[i]=loadImage("link stand"+(i)+".png");
}

```

you can change this to

```auto
for (int i=1; i<standingImages.length; i++) {
    standingImages[i - 1]=loadImage("link stand"+(i)+".png");
}

```

edit: also your frame count should probably go from 0-5 not 1-6

and as int acts as a floor when you call it on the frame as an index into the image array you can leave those but just change this as well

```auto
  if (frame>6) {
    frame=1.0;
  }

```

to

```auto
  if (frame>6) {
    frame=0;
  }

```

---

<div class="post-metadata">

**Author:** ![hannieways](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@hannieways](https://discourse.processing.org/u/hannieways)\
**Post date:** [May 3, 2019, 12:47am UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/5 "2019-05-03T00:47:06Z")

</div>

Now it seems to be running the standing animation for a few frames and then returns with NullPointerException still. Sorry if it seems like I’m being needy I’m just stumped lol. I really appreciate the help!

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [May 3, 2019, 12:53am UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/6 "2019-05-03T00:53:10Z")

</div>

i would check your file names. or check against this version which just has the adjustments i made with just some images which show the frame number etc.

> **[Uploadfiles.io - Download animationExp1.zip for free](https://uploadfiles.io/0obq7n26)**
>
> Upload files, for free, securely, anonymously, without limits. @UploadFilesFree

---

<div class="post-metadata">

**Author:** ![hannieways](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@hannieways](https://discourse.processing.org/u/hannieways)\
**Post date:** [May 3, 2019, 12:56am UTC](https://discourse.processing.org/t/game-error-nullpointerexception/10856/7 "2019-05-03T00:56:58Z")

</div>

Thank you a lot! I’ll keep working at it : )
