Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a File object to an array of File objects [duplicate]

Tags:

java

arrays

file

First, let me show you the code I have so far:

public void populateArray(){

    //NOTE: THIS METHOD SHOULD BE RUN FIRST. Many of the methods in this class rely on a populated file array to function.

    // This method will populate our file array.
    // First, we note the directory we want to pull files from.
    File folder = new File("HR");
    File dir = new File("");
    File file = new File("");

    //Then we fill an array with the list of documents in that directory
    //This will also include folders.
    int count = 0;
    allDocs = folder.listFiles();
    int fileCount = 0;
    int dirCount = 0;
    System.out.println("Displaying contents of directory...");
    while (count < allDocs.length){
        System.out.println("Found: " + allDocs[count]);
        count++;
    }
    System.out.println("Sorting directories and files separately...");
    count = 0;
    while (count < allDocs.length){
        if (allDocs[count].isDirectory()){
            dir = new File(allDocs[count].getPath());
            dirs[dirCount] = dir;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -Directory- array at position " + dirCount);
            dirCount++;
        }
        else{
            file = new File(allDocs[count].getPath());
            files[fileCount] = file;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount);
            fileCount++;
        }
        count++;
    }
    return;
}

files, dirs, and allDocs are arrays declared within the class outside of any methods.

allDocs is the array I use to get every directory and file in the directory of interest, HR. I do this with allDocs = folder.listFiles();

I want files to be the array to store all of the files in the directory HR, and I want dirs to be the array to store all of the directories in HR, but I don't want to store any of the contents of the directors in HR.

I try to do this with the loop under the println "Sorting directories and files separately."

The problem is here:

else{
            file = new File(allDocs[count].getPath());
            files[fileCount] = file;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount);
            fileCount++;
        }

It's here because the first element in allDocs is a file. The problem also occurs when the first element is a directory. The problem is a null pointer exception on the files[fileCount] = file; line, which I don't understand. "file" is not null, I just assigned it a value. Of course files[fileCount] is null in this case, but why does that matter if I'm assigning it a value?

Files declaration:

public class Methods
{

    private static File[] files;
like image 565
ozzymado Avatar asked Feb 17 '14 16:02

ozzymado


2 Answers

You might prefer to simplify:

List<File> fileList = new ArrayList<>();
List<File> dirList = new ArrayList<>();
while (count < allDocs.length){
    if (allDocs[count].isDirectory()){
        dirList.add(allDocs[count]);
        System.out.println("Document " + allDocs[count].getPath() 
            + " sorted into -Directory- array at position " + dirCount);
        dirCount++;
    }
    else{
        fileList.add[allDocs[count]);
        System.out.println("Document " + allDocs[count].getPath()
            + " sorted into -File- array at position " + fileCount);
        fileCount++;
    }
    count++;
}
dirs = dirist.toArray(new File[dirList.size()]);
dirCount = dirs.length;
files = fileList.toArray(new File[fileList.size()]);
fileCount = files.length;

Using a List<File> instead of File[] seems appropiate, as you do not know the array sizes in advance, and you need:

File[] files = new File[n];

if intending to fill in a File:

files[fileCount] = ...
like image 144
Joop Eggen Avatar answered Oct 20 '22 04:10

Joop Eggen


You might have forgotten to allocate the array - like this:

java.io.File [] files = new java.io.File[10];

If you only declared it like this:

java.io.File [] files;

files will be null and files[fileCount] will cause a NullPointerException.

As mentioned in the comments, you might like to change it to:

java.util.List<java.io.File> files = new ArrayList<>();

and add the files like:

files.add(file);

This way you won't have to know the number of entries in advance.

like image 28
Rainer Schwarze Avatar answered Oct 20 '22 04:10

Rainer Schwarze