# NullPointerException error

**URL:** https://discourse.processing.org/t/nullpointerexception-error/40167
**Category:** Coding Questions
**Created:** [December 15, 2022, 3:18am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167 "2022-12-15T03:18:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![onosecond](https://avatars.discourse-cdn.com/v4/letter/o/e274bd/32.png) [@onosecond](https://discourse.processing.org/u/onosecond)
#### Post date: [December 15, 2022, 3:18am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/1 "2022-12-15T03:18:06Z")

</div>

```auto
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!

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [December 15, 2022, 3:39am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/2 "2022-12-15T03:39:30Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![onosecond](https://avatars.discourse-cdn.com/v4/letter/o/e274bd/32.png) [@onosecond](https://discourse.processing.org/u/onosecond)
#### Post date: [December 15, 2022, 3:46am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/3 "2022-12-15T03:46:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![onosecond](https://avatars.discourse-cdn.com/v4/letter/o/e274bd/32.png) [@onosecond](https://discourse.processing.org/u/onosecond)
#### Post date: [December 15, 2022, 3:48am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/4 "2022-12-15T03:48:30Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [December 15, 2022, 4:25am UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/5 "2022-12-15T04:25:36Z")

</div>

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

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

```

to draw()

---

<div class="post-metadata">

### Author: ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)
#### Post date: [December 16, 2022, 3:16pm UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/6 "2022-12-16T15:16:27Z")

</div>

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

> **[requestImage() / Reference](https://processing.org/reference/requestImage_.html)**
>
> This function loads images on a separate thread so that your sketch doesn't freeze while images load during setup(). While the image is loading, its width and height will be 0. If an error occu…

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:

> **[loadImage() / Reference](https://processing.org/reference/loadImage_.html)**
>
> Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 16, 2022, 9:23pm UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/7 "2022-12-16T21:23:09Z")

</div>

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

---

<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: [December 17, 2022, 6:46pm UTC](https://discourse.processing.org/t/nullpointerexception-error/40167/8 "2022-12-17T18:46:57Z")

</div>

Hello @onosecond ,

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/6/5617e80c11f98534d1e4c4c43246bea80bee4306.png)

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](https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception.html)_

_[Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147)_ states:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147/1):
>
> Note that if you post extra code, you might be asked to narrow your problem down to a [Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/mcve). This just means that you should try to isolate your code into a smaller example.

`:)`
