Issue
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!
}
Solution
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.
Answered By - Rann Lifshitz