# selectInput() not opening file dialogue when used as intended

**URL:** <https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697>\
**Category:** Beginners\
**Created:** [January 17, 2022, 10:11pm UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697 "2022-01-17T22:11:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crysp](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Crysp](https://discourse.processing.org/u/Crysp)\
**Post date:** [January 17, 2022, 10:11pm UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/1 "2022-01-17T22:11:23Z")

</div>

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](https://processing.org/reference/selectInput_.html) for it and it had this example:

```auto
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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Crysp](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Crysp](https://discourse.processing.org/u/Crysp)\
**Post date:** [January 17, 2022, 10:58pm UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/2 "2022-01-17T22:58:22Z")

</div>

If this IS a bug, then I have to assume it’s because the “waiting for additional input” the documentation claims that the program is doing isn’t actually happening and continues running while `inputFile()` is still either opening the dialogue box or waiting for user input, until the main thread/program reaches the `draw()` function almost instantly and runs the first line of code using `p` and crashes since it’s still `null`.

I am not well versed in threads, so this is probably wrong, but I figure something along these lines is happening (of course, if I hadn’t made any mistakes to begin with).

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 17, 2022, 11:33pm UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/3 "2022-01-17T23:33:03Z")

</div>

here

(maybe it’s your missing size() command or something)

```auto

int state=0; 
PImage p;

void setup() {
  size(560, 560);
}

void draw() {
  //
  background(0);

  if (state==0) {
    state=1;
    selectInput("Select a file to process:", "fileSelected");
  } else if (state==1) {
    // wait
  } else if (state==2) {
    image(p, 0, 0);
  }
}

//------------------------------------------------------------

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    p = loadImage(selection.getAbsolutePath());
    state=2;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Crysp](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@Crysp](https://discourse.processing.org/u/Crysp)\
**Post date:** [January 18, 2022, 1:57am UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/4 "2022-01-18T01:57:42Z")

</div>

I didn’t mention this in the original post but I have `noloop()` in the setup, since I only need `draw()` to run once (get picture, alter it, put result in project folder). From the looks of it this solution only works if `draw()` is looped. Sorry about that

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 18, 2022, 6:22am UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/5 "2022-01-18T06:22:57Z")

</div>

- [null pointer exception when loading image in settings P3.1.1 - Processing 2.x and 3.x Forum](http://forum.Processing.org/two/discussion/16705/null-pointer-exception-when-loading-image-in-settings-p3-1-1#Item_1)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 18, 2022, 6:37am UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/6 "2022-01-18T06:37:44Z")

</div>

You can add your noLoop () after  
the lines that store the altered image

OR reset state to 0

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [January 18, 2022, 9:45am UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/7 "2022-01-18T09:45:33Z")

</div>

All of Processing’s select???(…) methods are executed on a separate thread. It means that the first time draw() is executed happens before the fileSelected(…) method. So if you want to process the file in draw() then you should not use noLoop().

Try this sketch out it shows what I mean

```auto
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() {
  background(255);
  if (p != null) {
    // Never executed
    println("Processing image");
  } else {
    println("NO IMAGE");
  }
  noLoop(); // Try again without this line
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 18, 2022, 10:07am UTC](https://discourse.processing.org/t/selectinput-not-opening-file-dialogue-when-used-as-intended/34697/8 "2022-01-18T10:07:43Z")

</div>

I would never make a Sketch without size()
