NullPointerException error

Question [] questions; //array of objects for the question class
Screens sc; // object of the screen class
Answers ans;
final int stateFirst=0; //cannot be changed
final int stateGameOver=1;
final int stateq1=2;
final int stateq2=3;
final int stateq3=4;
final int stateq4=5;
final int stateq5=6;
int currentState=stateFirst;//setting the current state to statefirst so that the game starts on the first screen
int score=0;
PImage [] left;
PImage [] right;

void setup() {
  size(600, 600);

  left=new PImage[5]; //creating arrays of images
  left[0]=loadImage("first.jpeg");
  left[1]=loadImage("second.jpg");
  left[2]=loadImage("third.jpg");
  left[3]=loadImage("fourth.jpg");
  left[4]=loadImage("fifth.jpg");
  right=new PImage[5];
  right[0]=loadImage("firstb.jpg");
  right[1]=loadImage("secondb.jpg");
  right[2]=loadImage("thirdb.jpg");
  right[3]=loadImage("fourthb.jpg");
  right[4]=loadImage("fifthb.jpg");
  
  for (int i=0; i<5; i++) {
    image(left[i], 50, 200, 250, 400);
    image(right[i], 300, 200, 250, 400);
  }
  
  ans=new Answers(left, right);
  sc=new Screens();
  String [] text=loadStrings("questions.txt");//load the file
  questions=new Question[text.length];

  for (int i=0; i<questions.length; i++) {
    String [] q=split(text[i], "?"); //split the text file whenever we hit a question mark, getting the questions by using the indices
    questions[i]=new Question(q);
  }
}

void draw() {
  sc.showState(); //calling the show state method

  for (int i=0; i<questions.length; i++) { //calling the showQuestionState() inside a for loop that runs as many times as the length of the file
    showQuestionState();
  }
}

void keyPressed() { //if spacebar is pressed, we move to the question screen
  if (key==' ') {
    currentState=stateq1;
  }
}
void mousePressed() { //using mouse pressed event handler so that when we press the mouse on the questions screen inside the specific answer region, it switches to the next question.
  if (mouseX>=50 && mouseX<=550 && mouseY>=200 && mouseY<=600) {
    if (currentState==stateq1) {
      currentState=stateq2;
    } else if (currentState==stateq2) {
      currentState=stateq3;
    } else if (currentState==stateq3) {
      currentState=stateq4;
    } else if (currentState==stateq4) {
      currentState=stateq5;
    } else if (currentState==stateq5) {
      currentState=stateGameOver;
    }
  }
}


void showQuestionState() { //function used to show the respective questions in order depending on what the state is
  ans.answerMechanism();
  if (currentState==stateq1) {
    questions[0].showQuestion();
  }
  if (currentState==stateq2) {
    questions[1].showQuestion();
  }
  if (currentState==stateq3) {
    questions[2].showQuestion();
  }
  if (currentState==stateq4) {
    questions[3].showQuestion();
  }
  if (currentState==stateq5) {
    questions[4].showQuestion();
  }
}
class Answers {
  PImage []answer1;
  PImage[] answer2;

  Answers(PImage [] a1, PImage [] a2) {
   answer1=a1;
   answer2=a2;
  }

  void answerMechanism() {
    if (currentState==stateq1 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
      score++;
    } else if (currentState==stateq2 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
      score++;
    } else if (currentState==stateq3 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
      score++;
    } else if (currentState==stateq4 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
      score++;
    } else if (currentState==stateq5 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
      score++;
    }
  }
  void showAnswerStructure(){
    for(int i=0;i<answer1.length;i++){
    answer1[i].resize(250,400);
    }
    for(int i=0;i<answer2.length;i++){
       answer2[i].resize(250,400);
    }
  }
}

Hello everyone,
I ran into a problem with my project. Right after I added the images to the setup, my code won’t run and the NullPointerException error appears. Any idea what might have caused it? The code was running fine before adding the images.
Thanks in advance!

Hi,

Idea of setup() is to load stuff and make other time consuming preparations. And have the actual functionality in draw(), after all that loading and preparations are done.
Processing does not promise that all the stuff is loaded while inside setup() code, but draw will not start before setup is finished and all loading is done.

My guess is that processing has not loaded all the images and you continue to use them anyways causing NullPointerException.

1 Like

I tried loading the images in draw(), but I still get the same error. Do you have any recommendation?
Thanks for responding!

I deleted this part of the code and now the code runs but the images wont load.

for (int i=0; i<5; i++) {
    image(left[i], 50, 200, 250, 400);
    image(right[i], 300, 200, 250, 400);
  }

Loading is supposed to be in setup(), but you should use those files in draw(). So try moving

for (int i=0; i<5; i++) {
    image(left[i], 50, 200, 250, 400);
    image(right[i], 300, 200, 250, 400);
  }

to draw()

You may benefit from using requestImage() during the setup() for this scenario.

PS: The Question Class is not in the posted code snippet provided so I couldn’t test it on my system.

Best of luck!

also FYI:

please make sure the 10 images are named correctly and are in the data folder of your Sketch

the naming is case sensitive and it’s a different whether it’s jpg or jpeg or JPG …

the file names must be exactly the names in the code such as left[0]=loadImage("first.jpeg");

for example, here you have jpeg, the other names are with jpg

1 Like

Hello @onosecond ,

It also helps to share where you are getting the errors:

This is usually highlighted.

In this case there are two messages in the console:

  • The image was missing.
  • It generated a NullPointerException when trying to display it.

You must then interpret the messages and try to resolve what is causing the issue.

References:
Why do I get a NullPointerException? - Processing 2.x and 3.x Forum

Guidelines—Asking Questions states:

:)

1 Like