1

please help me. I want to load multiple images into this processing sketch without knowing the datanames. So that i can always just put .png images into the data folder and the program automatically loads them in. I've searched in some forums but didn't find anything except some code that I've already used but it doesn't run properly.

As soon as the program starts it gives me a NullPointerException at image();

This is the consoleoutput:

4096
D:\Program Files\processing-3.3.7\PROJECTS\Blendertutorial\data
[0] "1.png"
[1] "2.png"
[2] "3.png"
[3] "4.png"
[4] "5.png"
[5] "6.png"
[6] "7.png"

Also why is the folder.list(); output such a huge number? I only have 7 images in there...

import java.io.File;
String fileExtension = ".png";

java.io.File folder = new java.io.File(sketchPath("/PROJECTS/Blendertutorial/data"));
java.io.FilenameFilter extfilter = new java.io.FilenameFilter() {
  boolean accept(File dir, String name) {
    return name.toLowerCase().endsWith(fileExtension);
  }
};

PImage images;
String[] imageNames;
int i=0;
long folderInhalt = folder.length();

void setup(){
  size(500,500);
  println(folder.length());
  println(folder);
  printArray(imageNames);

  imageNames = folder.list(extfilter);
}

void draw(){
  if(mousePressed){
    images = loadImage(folder+"/"+imageNames[0]);
    println(images);
    println(imageNames[i]);
    delay(200);
    i++;
  }
  image(images,0,0);      //NULL POINTER EXCEPTION!
}
coder
  • 8,346
  • 16
  • 39
  • 53
  • Not directly related to your problem, but you should not load images inside the `draw()` function like that. Load them from the `setup()` function instead. Otherwise you'll load an image 60 times per second, which will cause your computer to catch on fire. – Kevin Workman May 07 '18 at 18:37
  • Thanks. I only did it in this example because im calling delay() and because i dont want it to "preload" bu load when its needed. – Oliver Tworkowski May 07 '18 at 20:47
  • 1
    The problem is that the mouse will be pressed for more than one frame, so you're constantly reloading the image. At the very least you should cache your images so you don't reload them more than once, but most likely you should just load them all during setup. – Kevin Workman May 07 '18 at 20:49

4 Answers4

1

What happens in your code when draw() is called, but mousePressed is false?

Consider your code:

PImage images;
...
...
...

void draw(){
  if(mousePressed){
    images = loadImage(folder+"/"+imageNames[0]);
    println(images);
    println(imageNames[i]);
    delay(200);
    i++;
  }
  image(images,0,0);      //NULL POINTER EXCEPTION!
}

You have declared images but have not instantiated it.

In the case of mousePressed==false, images will remain null.

This behavior can explain your NullPointerException - you are invoking the image method with a null valued parameter.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
1

why is the folder.list(); output such a huge number? I only have 7 images in there...

You aren't calling folder.list(). You're calling folder.length(). This function returns the size of the file, which is undefined for folders. More info can be found in the Java API.

Also, this is not directly related to your problem, but you should not load images inside the draw() function like that. Load them from the setup() function instead. Otherwise you'll load an image 60 times per second, which will cause your computer to catch on fire.

Moving your loading to setup() will also fix your NullPointerException problem, because you'll no longer be drawing null images when the mouse is not pressed.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0
public void readImagesPath(String dir){
   File folder = new File(dir);
   File[] listOfFiles = folder.listFiles();

    for (File file : listOfFiles) {
      if (file.isFile()) {
         System.out.println(file.getName());
      }
    }
 }

With this code you can retrive the image paths and then you can load and draw them.

helloworld
  • 59
  • 1
  • 9
0

I have this and it seems to work (based on the file selection example)

void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
img = loadImage(selection.getAbsolutePath());
}
}