Hello. I am on a Macbook running macOS Monterey 12.0.1 and I found an issue (or a mistake I wasn’t able to solve) with selectInput()
in Processing. My goal was to use it to pass an image to the program which I then do what I wanted to do with, and then save the result into the project folder for me to look at.
I looked up the documentation for it and it had this example:
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
}
}
I altered the line in the else statement to just set a PImage variable p
as the output of the function loadImage()
with selection.getAbsolutePath()
as the input (for me to then use in draw()
), like this:
PImage p;
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
p = loadImage(selection.getAbsolutePath());
}
}
void draw() {
//the code I manipulated p with
}
The theory is I run this, the file dialogue opens, I choose a file, it gets passed to fileSelected
where the file is put in the p
variable for me to then use. However, when I run it, the dialogue doesn’t open, inputFile()
sends null
to the fileSelected()
function where it is put into p
and the code in draw()
throws a nullPointerException
. Also worth noting that the first example (the code I found in the documentation) also had this issue and always printed null
and the dialogue box wouldn’t show up.
Strangely enough, if I add a while
loop in the draw()
function that prints something every cycle, the dialogue pops up and the program works normally, like this:
void draw() {
while (p==null) {
println("waiting for image");
}
//other code cut out
}
This doesn’t work, however, if the while
loop has nothing in it (like while(p==null){}
).
My theory is that the program doesn’t stop for the dialogue to find the image, but adding the while
loop ‘distracts’ it for the dialogue to pop up and the user to select an image, which is put into the p
variable and thus stops the loop and the program can continue normally. The documentation also says: “The callback is necessary because of how threading works”, which might support my theory (main thread running draw()
doesn’t wait for thread running selectInput()
to get a file).
Is there some monumental mistake I made here? This doesn’t feel like it’s bugged and I found a workaround. I feel like I’m missing something. Any help is appreciated because I feel as if having a while
loop there only to pad for time is pretty bad design. Thanks
EDIT: After testing my solution a little bit I found out after a few runs it stops working and doesn’t go past the loop (similar to the behavior if I just left the loop empty instead of printing something) and I have to restart Processing to get it to work again. This solution screams janky so I’m just going to remove it until I hopefully find a solution