Loading...
Logo
Processing Forum
Hi guys,

I am trying to make my processing file load a data file on startup. I am using the selectFolder() command and this is working:

selectFolder("Select folder containing constant input spreadsheets e.g. deadloads.csv", "folderSelected");

However, when I try to choose the initial folder to be the same as the sketch folder, I am having trouble.

File programFolder = file(sketchPath(""));
selectFolder("Select folder containing constant input spreadsheets e.g. deadloads.csv", "folderSelected", programFolder);

How do I convert a string to a File data type?

Thanks in advance

Replies(7)

Chane this
File programFolder = file(sketchPath(""));

to

File programFolder = new File(sketchPath(""));
Perfect, thanks! 
Sorry, just a quick follow up question. This command is giving me the Processing install folder, rather than the sketch folder (i.e. "C:\Program Files\processing-2.0b8"). Is there a way of getting the sketch folder? Is this problem fixed when I export as a .exe standalone file? 

Thanks,

Chris

Note that sketchPath (the variable) is the same thing than sketchPath("").
And as per the name, it does return the sketch path, not the installation path.
Perhaps you made an error somewhere?
Try:
Copy code
  1. File programFolder = new File(sketchPath);
  2. println(programFolder.getAbsolutePath());

Hi PhiLho,

Thanks for your response. When I use the following code:
Copy code
  1. File programFolder = new File(sketchPath(""));
  2.   println(programFolder.getAbsolutePath());
The following is displayed in the console:

"C:\Program Files\processing-2.0.1"

This is not the sketch path, but the processing install path.

If I use your suggestion:
Copy code
  1. File programFolder = new File(sketchPath);
  2. println(programFolder.getAbsolutePath());
I am given an error message:

java.lang.NullPointerException

Any ideas? I have just downloaded the latest processing version (2.0.1) to make sure that that is not the issue. 

Thanks,

Chris

That's strange.
From a freshly downloaded 2.0.1 (32-bit Windows), if I run both mini-sketches as is (in static mode, no setup()), I get:
C:\tmp\untitled4875277725524312515sketches\sketch_130708a
or similar. That's because I haven't saved the sketch, so it points to the temporary sketch folder it creates on the fly.

Ah, a guess...
Yes, I can reproduce your problem with:
Copy code
  1.     File programFolder = new File(sketchPath);
  2.    
  3.     void setup()
  4.     {
  5.       println(programFolder.getAbsolutePath());
  6.     }
It is a very common error, trying some initializations at the global level (often with variables like width, too).
Code at this level is run before anything else, but width and sketchPath are initialized only when setup() is called. So they are zero and null, respectively, at this time.
Correct code:
Copy code
  1.     File programFolder;
  2.    
  3.     void setup()
  4.     {
  5.       programFolder = new File(sketchPath);
  6.       println(programFolder.getAbsolutePath());
  7.     }

Sorted. As you correctly say, the sketchPath variable needs to be called from within setup() or later. Thanks for taking a look.

Chris