reading filesnames from a folder

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:

    // we'll have a look in the data folder
    java.io.File folder = new java.io.File(dataPath(""));
    
    // list the files in the data folder
    String[] filenames = folder.list();
    
    // get and display the number of jpg files
    println(filenames.length + " jpg files in specified directory");
    
    // display the filenames
    for (int i = 0; i < filenames.length; i++) {
      println(filenames[i]);
    }
    
  • Processing isn't aware of its own save folder before setup() begins!
    You should initialize such classes only after setup() started!

  • edited December 2013 Answer ✓

    Good spot @GoToLoop. Here's a version that's closer to the original that seems to run OK:

    File dir; 
    File [] files;
    
    void setup() {
      dir= new File(dataPath(""));
      files= dir.listFiles();
    }
    
    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);
        }
      }  
    }
    
  • edited December 2013

    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();

    import java.io.File;
    
    File folder;
    String [] filenames;
    
    void setup()
    {
      java.io.File folder = new java.io.File(dataPath(" "));
    }
    
    void draw()
    {
      filenames = folder.list();
      println(filenames.length + ".properties.ser");
    
      for(int i = 0; i <= filenames.length; i++)
      {
        println(filenames[i]);
      }
    }
    

    cheers!

  • edited December 2013

    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.

    java.io.File folder = new java.io.File(sketchPath(""));
    

    Hope that helps.

  • Answer ✓

    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.

Sign In or Register to comment.