We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi,
i am trying to read file names from a folder to populate a dropdown list in cp5. however, i am receiving a null pointer exception in my loop. i am not sure what the problem is. i appreciate any time, or just an extra pair of eyes, you can lend...
import java.io.File;
File dir = new File("folderName");
File [] files = dir.listFiles();
// here, if i try to print the contents of the files list: println(dir) or println(files), i also receive errors
void setup()
{
}
void draw()
{
for (int i = 0; i <= files.length - 1; i++)
{
String path = files[i].getAbsolutePath();
if(path.toLowerCase().endsWith(".properties.ser"))
{
println(path.toLowerCase() + ".properties.ser", i);
}
}
}
Answers
This seems to work for me (from the wiki:
Processing isn't aware of its own save folder before setup() begins!
You should initialize such classes only after setup() started!
Good spot @GoToLoop. Here's a version that's closer to the original that seems to run OK:
thanks all. i'll try these suggestions out.
hi velvet, how do yo not get a null pointer exception at line 10? this code doesn't run for me although it seems it should.
i also receive the same with your first suggestion... a null pointer exception at filenames = folder.list();
cheers!
deleted the earlier message because i had a bout of idiodacy.
however, using dir = new File(dataPath(" "));, how does one navigate to a level up to where the .pde is stored? i have tried File(../dataPath(" ")) and File(dataPath(../" ")) and other combinations to no avail.
thanks in advance.
Hi, Using
sketchPath()
should get you there.Hope that helps.
There should be no space between double quotes in dataPath("")
And if you need the sketch's path, use the sketchPath variable (or the sketchPath() method, with a file name).
thanks velvet and philo.