Getting the length of the static file array? Always return null

advertisements
public PImage[] images;
public static File[] wallTiles = loadFolder("data/tiles/walls");
public static File[] floorTiles = loadFolder("data/tiles/floors");
public static int totalFiles = wallTiles.length+floorTiles.length;

private static File[] loadFolder(String string)
{
    File files = new File(string);

    File[] pngFiles = files.listFiles(new FilenameFilter()
    {
        public boolean accept(File directory, String fileName)
        {
            return fileName.endsWith(".png");
        };
    });
    return pngFiles;
}

Line 4, where totalFiles is assigned, always fails because wallTiles is null. I want to return the totalFiles in this class (called Art), so that I can do things like randomly choose a wall or floor texture for a game I'm working on. Thanks for listening.


From the API http://docs.oracle.com/javase/6/docs/api/java/io/File.html#listFiles()

... Returns null if this abstract pathname does not denote a directory ...

So check if the directory exists and if there are files.

private static File[] loadFolder(String string)
{
    File files = new File(string);
    if(!files.exists() || !files.isDirectory()){
        System.out.println("Directory: "+string+" does not exists");
        return new File[0];
    }

    File[] pngFiles = files.listFiles(new FilenameFilter()
    {
        public boolean accept(File directory, String fileName)
        {
            return fileName.endsWith(".png");
        };
    });
    return pngFiles;
}