tags:

views:

43

answers:

2

Basically, I have a directory with some files in it. In run configurations I am trying to put the directory as an arguement like so: \(workspacename\directory. Then, the following code should create a list of all the files in that directory:

String directory = args[1];
File folder = new File(directory);
File[] allFiles = folder.listFiles();
ArrayList<File> properFiles = null;
for (File file: allFiles) {
    if(file.getName().endsWith(".dat")){
    properFiles.add(file);
    }
}

the problem i'm facing is that for some reason allFiles is null.

+4  A: 

I'll take a guess at what your problem might be:

If your argument is a relative path (as opposed to an absolute path, staring with "/" or "c:/" for example), keep in mind that files will be relative to the working directory of the application.

So new File(directory) will be relative to wherever the application is started. In Eclipse the default working directory is in the project. So if your project is in the top level of the workspace, it will be something like workspacename/project.

You can try printing out folder.getAbsolutePath(), folder.exists() and folder.isDirectory() to help diagnose your problem.

Ash
A: 

The javadocs say listFiles() will return null if the directory does not actually exist (among other things):

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

Debug by verifying (debugger or printf) the args[1] value.

Also, it looks like you might be trying to use a substitution variable to insert the workspace location in the path. If so, again, you need to verify (via debugger or printf) that the placeholder is getting replaced properly.

Bert F