Game Error (NullPointerException)

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)

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;

}
1 Like

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

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

shouldn’t it be

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

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

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

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

you can change this to

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

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

to

  if (frame>6) {
    frame=0;
  }
1 Like

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!

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.

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