How to get the filepath from selectInput()

edited October 2016 in Questions about Code

I have setup the callback function and it opens the File open dialog and I can choose the file but I don't understand how to get access to the filename. I have tried using a global variable that I set in the callback function but when I try to open a file I get a long list of java exceptions, the first couple of lines say

null does not exist or could not be read

########## EXCEPTION IN EVENT HANDLER

An error occured during execution of the eventhandler:CLASS: EchoTapperPatchLibrarian METHOD: LoadButton_click1 Caused by java.lang.NullPointerException . etc. .

The section of the code where the selectInput() is called is

BufferedReader reader;

String line;


 // Open the file

  selectInput("Select a file to process:", "fileSelected");

  println(filename);

  reader = createReader(filename);

 ...

The program actually opens the file but I'm concerned by the exceptions!

The callback function is

void fileSelected(File selection) {

if (selection == null) {

println("Window was closed or the user hit cancel.");

 } 

else {

println("User selected " + selection.getAbsolutePath());

filename = selection.getAbsolutePath(); // filename is a global String variable

 }
}

Does anyone know how to use selectInput() and selectOutput() properly?

Thanks for any help :-)

Phil.

Tagged:

Answers

  • After further investigation, I found that if you put the file processing code in the callback function, it works ok that way.

  • That is because the code handling the file chooser window runs in a separate thread, so after calling selectInput() it will not wait for this to finish but instantly go to the next statement. In other words at the time you work with filename there it is not yet set.

  • Thanks fjen. That's what I finally figured out! I'm not as quick a study as I was in my youth ;-)

  • i have the same problem... how did you solve it?

  • Hi, sorry haven't been on here for a while. Try this ( Hopefully you can tease out the relevant parts for your application. )

    void fileSelectedforOutput(File selection) {
      PrintWriter output;
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } 
      else {    // Error handling in case of bad filenames etc. try ... catch ???
        try {
          filename = selection.getAbsolutePath();
          output = createWriter(filename);
          output.println(maxPatches); // First entry in the file is the number of patches that follow, one per line
    
          for (int i = 0; i < maxPatches; i++)
            output.println(patchList[i].output()); // Write each patch as a line of comma separated values
          output.flush();
          output.close();
        }
        catch (Exception e) {
          println("Computer says No");
        }
      }
      String[] qpath = splitTokens(filename, System.getProperty("file.separator"));
      int last = qpath.length;
      MessageBox2.setText(qpath[last-1]);
      MessageBox2.setTextAlign(GAlign.LEFT, GAlign.CENTER);
    }
    
  • so what should I do @fjen? I need process continue after file selection , how to push sketch to wait until file selection completed

  • Answer ✓

    One way to do things:

    boolean fileSelectedyet=false;
    void setup() {
      size(400, 600);
      selectInput("Select a file to process:", "fileSelected", dataFile("name_of_file"));
    }
    
    void fileSelected(final File selection) {
      if (selection == null)
        println("Window was closed or the user hit cancel.");
      else
        println("User selected " + selection.getPath());
    
      fileSelectedyet=true;
      fill(255);
    }
    
    void draw() {
      background(0);
    
      if (!fileSelectedyet){
        text("No file selected!", width/2, height/2);
      }
      else{    
        fill(150,150,25);
        textSize(28);
        text("File chosen!", width/2, height/2);
      }
    }
    

    I hope this helps,

    Kf

Sign In or Register to comment.