We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Attempt 1:
PImage img;
void setup() {
selectInput("Select a file to process:", "fileSelected");
size(img.width,img.height); //this returns a null pointer exception
}
void draw(){
background(img);
}
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());
}
}
Why should I get a null pointer exception? Isn't "img" a global variable? Its set in the function "filelected"
Attempt2:
PImage img;
void setup() {
frame.setResizable(true);
selectInput("Select a file to process:", "fileSelected");
}
void draw(){
background(img); // This returns a null pointer exception
}
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());
size(img.width,img.height);
}
}
Another nullpointer exception.
Attempt 3 :
PImage img;
void setup() {
frame.setResizable(true);
selectInput("Select a file to process:", "fileSelected");
}
//void draw(){
// background(img);
//}
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());
size(img.width,img.height);
background(img);
}
}
This kind of works believe it or not. At least is gets to the file selector and actually displays the picture. But if I uncomment the draw(), it turns black. What is going on here? Intuitively is seems the global "img" would be set in the function "fileSelected" and shouldn't be throwing a null pointer exception.
Edit: this looks awful when it posts. There's no way to format the code in the post?
Answers
I found this code in this comment thread :
http://forum.processing.org/two/discussion/comment/42788/#Comment_42788
This works and the only thing I can figure is draw() is running while file selected is running. In other words, the program doesn't wait for fileSelected() to complete before doing its stuff. It begins immediately . Are all these functions running simultaneously like in different threads?
Here's the code that works and the only thing it does different make sure img has been assigned a value:
Yes, exactly.
I also had a problem when I referenced img in setup() after the selectinput() call. So I assume everything starts at once even before setup() completes?
I also found this. I haven't seriously programmed in about 20 years and if there was multi threading it was in its infancy. I found this :
Its a whole new world. I'm so used to functions occurring in order, this will take some practice. K&R must be spinning.
http://forum.Processing.org/two/discussions/tagged?Tag=selectinput()
you wrote
but the code does exactly this: draw() runs and while there is no image selected , it runs doing nothing (it waits)
The marker imageSelected controls this behaviour (the ! sign means not).
but this is how you do it: let draw() run on empty till the other thread is done and is signalling this by setting imageSelected to true
Chrisir
Here an old code that do load and resize the window to fit the image size:
@_vk, thanks for the example. That does perfectly something I've wanted to do for a long time.
:) good!