Getting a NullPointerException when trying to simply display an image in draw()

Hello,

I have this issue with Processing 4.0b6/Windows.

This ultrasimple sketch triggers a NullPointerException when trying to display the image within draw(). Otherwise when displaying it within setup() it works fine. Also, the same image has been used in numerous other sketches. The sketch name doesn’t contain any special character.

What’s wrong??? :thinking:

PImage originImg;

void setup() {
  size(800, 600);
  noStroke();
  frameRate(60);
  PImage originImg = loadImage("flood_red_blue_1920x1080_bordure.png");
  //image(originImg, 0, 0, width, height); // works flawlessly if executed here
}

void draw() {
  image(originImg, 0, 0, width, height); // NullPointerException if executed here
}

Got it! It’s because I wrote PImage again on the line with loadImage!

2 Likes

Yes Sir. There you create a local variable that shadows the global variable.

2 Likes