0

I seem to be having an issue with not properly syntaxing my code, but as I've just started out with learning I seem to be missing the error. It's a homework assignment, where I need to use an Array of JxploreFile-objects. This is the part of the code I'm having trouble with:

private JxploreFile[] getSubFolders()
{
    File subFiles[];
    subFiles = file.listFiles();

    File subFolders[];
    int p = 0;
    for(int i = 0; i < subFiles.length; i++)
    {
        if(subFiles[i].isDirectory() == true)
        {
            Array.set(subFolders, p, subFiles[i]);
        }
    }

    JxploreFile foldersToReturn[] = new JxploreFile[subFolders.length];
    for(int i=0; i < subFolders.length; i++)
    {
        foldersToReturn[i] = new JxploreFile(subFolders[i]);
    }
    return foldersToReturn;
}

Specifically, the for-loop where I'm trying to add the files marked as .isDirectory into a new Array. I've also tried other methods by placing each new file coming from the subFiles Array manually into the subFolders Array by declaring indexnumbers, but this also turned out faulty. At this point I'm out of ideas and I hope there is someone who can point me out the obvious, as I'm probably missing something reallly basic.

Edit: I'm sorry for the incomplete post, It's the first time I actually post here as I usually try to filter my own problems out of the posts of others. The error I got was indeed that 'subFolders' had not been initialized yet, which I didn't understood because on the sixth line I wrote File subFolders[]; which as far as I know should declare the variable subFolders to become an Array, or is this where I went wrong? Also, my question might not have been specific enough, I'm looking for what causes the error (which I didn't mention at all): why 'subFiles' wasn't initialized.

4
  • So.. what is causing you trouble in your code ? Commented Feb 7, 2015 at 16:24
  • "Trouble" is too general. What errors do you get? Add them to your question. What do you expect to happen, and what do you get instead? Commented Feb 7, 2015 at 16:26
  • 1
    The subFolders has not been initialized. Commented Feb 7, 2015 at 16:26
  • updated the main post with a more specific question and the error I was getting with the method. Commented Feb 7, 2015 at 17:05

2 Answers 2

1

The array subFolders has not been initialized properly. In order to use the array in the Array.set method it must be initialized and allocated with a size.

An alternative approach for this is to use a List instead. Lists are good when you are working with data that is more dynamic e.g. when you do not know the size of the array. Then you can simplify your code like this:

File[] subFiles = file.listFiles();

// Create the list
List<JxploreFile> subFolders = new ArrayList<>();

// Add all the sub folders (note that "file" is a bit magic since it 
// is not specified anywhere in the original post
for (File subFile : file.listFiles()) {
    if (subFile.isDirectory()) {
        subFolders.add(new JxploreFile(subFile));
    }
}

// Return an array
return subFolders.toArray(new JxploreFile[subFolders.size()]);

You can also simplify the whole thing even further by using a Java 8 stream like this:

return Arrays.stream(file.listFiles())
             .filter(File::isDirectory)
             .toArray(JxploreFile[]::new);

For more info:

Sign up to request clarification or add additional context in comments.

3 Comments

could you explain to me why the subFolders Array is not initialized properly, because I did intitialize it on the sixth line, didn't I?
@Ciphra, you have declared it File subFolders[]; but not initialized it with a value. Check the link in the answer on the difference between initializing and declaring Creating Objects in Java.
I see what you mean now, what I'm trying to do here is basicly not possible in it's current structure because it requires me to define a length for an Array that I don't know until I've filled it. And that's why you showed it with the use of lists. Thanks for the patience in answering, I'm a little slow with these things I suppose..
1

There is no question in your post, but anyway here the problems I found in your code :

File subFolders[];
int p = 0;
for(int i = 0; i < subFiles.length; i++)
{
    if(subFiles[i].isDirectory() == true)
    {
        Array.set(subFolders, p, subFiles[i]);
    }
}

when calling Array.set you never initialized subFolders which will throw a NullPointerException.

Also, you dont need to do

if(subFiles[i].isDirectory() == true)

you can simply do

if(subFiles[i].isDirectory())

as subFiles[i].isDirectory() is already a condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.