# Wait for selectFolder()

**URL:** <https://discourse.processing.org/t/wait-for-selectfolder/22122>\
**Category:** Coding Questions\
**Created:** [June 24, 2020, 5:55am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122 "2020-06-24T05:55:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [June 24, 2020, 5:55am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122/1 "2020-06-24T05:55:46Z")

</div>

I am writing a program to do mass image editing and would like to have the end user select an input and output folder when the program starts.

I tried using `selectFolder("Select a input folder:", "inputFolder");` and it opened the folder selection thing, but my code continues to run before a folder is selected, leading to a null error where one of my commands tries acting on the not yet initiated `String iPath`

> **Minimized code example:**
>
> ```auto
> import java.io.File;
> 
> String inputPath;
> 
> void setup() {
> selectFolder("Select a input folder:", "inputFolder");
>   
> CommandIWantToRunAFTERFolderIsSelected(inputPath);
> }
> 
> void inputFolder(File selection) {
> if (selection == null) {
> javax.swing.JOptionPane.showMessageDialog(null, "Input Window was closed or the user hit cancel.", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
> exit();
> } else {
> inputPath = selection.toString();
> println("User selected " + selection.getAbsolutePath());
> }
> }
> 
> void CommandIWantToRunAFTERFolderIsSelected(String path){
> if (path == null){
> println("Process crash due to null interperetation");
> } else {
> println("no crash as command ran after string initiated");
> }
> }
> 
> ```

Running the above code without modification, you will see that the command that interprets the path runs before a file path is selected.

How would I fix this issue

---

<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:** [June 24, 2020, 7:34am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122/2 "2020-06-24T07:34:04Z")

</div>

Basically in setup () have only size and SelectFolder

In draw() do what you want to achieve but with an if clause around it like if(ipath!=null) {

---

<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:** [June 24, 2020, 9:46am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122/3 "2020-06-24T09:46:44Z")

</div>

Prior to PS3 the `select????` methods were modal i.e. blocked the main thread (which how Java) works. In pS3 the Processing developers changed it so that they are executed on a separate thread it means the main thread no longer stops.  
There is no option in Processing to make them behave modally so you have 3 options

1. Simply test the reference and only use the selected folders when they are not null i.e.

```auto
    if(inFolder != null){
      // safe to use inFolder
    }

```

1. Write your own code to access the Java Swing file dialog boxes
2. Use the equivalent functions in G4P because they are modal. I added these when PS3 was released.

---

<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:** [June 24, 2020, 10:41am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122/4 "2020-06-24T10:41:19Z")

</div>

> [@F53](#):
>
> ```auto
> String inputPath;
> 
> void setup() {
> selectFolder("Select a input folder:", "inputFolder");
>   
> CommandIWantToRunAFTERFolderIsSelected(inputPath);
> }
> 
> ```

> **[delay() / Reference](https://processing.org/reference/delay_.html)**
>
> The delay() function causes the program to halt for a specified time. Delay times are specified in thousandths of a second. For example, running delay(3000) will stop the program for thr…

```auto
String inputPath;

void setup() {
  selectFolder("Select an input folder:", "inputFolder");

  while (inputPath == null) delay(1);

  CommandIWantToRunAFTERFolderIsSelected(inputPath);
}

```

---

<div class="post-metadata">

**Author:** ![F53](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/f53/32/7118_2.png) [@F53](https://discourse.processing.org/u/F53)\
**Post date:** [June 25, 2020, 12:02am UTC](https://discourse.processing.org/t/wait-for-selectfolder/22122/5 "2020-06-25T00:02:48Z")

</div>

> [@GoToLoop](#):
>
> `while (inputPath == null) delay(1);`

I really like this solution to the problem, but I marked @quark’s reply to the solution as it gave multiple, general solutions.
