NullPointerExeption with loading images

Hi, i need to load 4 images and draw them in a window, but i get this error.

here’s my code

PImage image;
PImage image1;
PImage image2;
PImage image3;
String path ="";
void setup() {
  size(360, 240);
  text("click window to load an image", 100, 120);
}

void handleImage(File selection) {
  if (selection==null) {
    println ("Error: 404");
  } 
  else {
    path = selection.getAbsolutePath();
    image =  loadImage(path);
    image1 = loadImage(path);
    image2 = loadImage(path);
    image3 = loadImage(path);
    surface.setSize(image.width * 2, image.height * 2);
  }
}
 
void mouseClicked() {
  image = null;
  selectInput("select an image", "handleImage");
}

void draw() {
  if (image != null) {
    image(image,          0,           0           );
    image(image1,         image.width, 0           );
    image(image2,         0,           image.height);
    image(image3,         image.width, image.height);
    //image.filter(GRAY);
    //treshold(image1, 1, image.width, 0           );
    //ditherio(image2, 1, 0,           image.height);
    //ditherio(image3, 4, image.width, image.height);
  }
}
1 Like

Which line is highlighted when it gives the error?

1 Like

Change this to if (image3 != null). Notice you are calling loadImage four times to load the same resource. Instead, you could do: image3=image.get() as this is faster. The mean reason of your error is that you check for null content on only one object and you assume the other ones “should” be ok.

Kf

1 Like