0

I'm trying to recursively get a directory structure in Java, on an Android emulator, with what I think is a fairly straightforward bit of code:

public void list(File file) 
{
    if(file.isDirectory())
    {
       File[] children = file.listFiles();
       for(int i = 0; i < children.length; i++)
       {
           Log.d(TAG, children[i].getName());
           list(children[i]);
       }
    }
}

It works fine (so far) unless I run it in the root of the emulator's SD card, at which point it throws a null pointer exception. LogCat claims it's thrown when the method calls itself, but I have to wrap the entire for loop in a try/catch block in order to catch the exception.

I've tried about half a dozen permutations on the same sort of theme, and the above is the most bland and safe I can come up with, but they all throw this error. I'm either doing something silly, or there's an odd object lurking in the root of the emulator's SD card which reports as a directory but returns a bogus value with .listFiles(). That .android_security certainly looks a bit shifty.

Would anyone be so kind as to tell me which, if either of them, it is?

1
  • 2
    Others answered the question (null check), but here is the reason. File.listFiles() will return null (as opposed to an empty array) if it cannot list files for some reason. One example could be security permissions forbid it, another could be an invalid symlink. For some reason, this does not throw java.io.IOException despite the fact that other methods on the class do in similar situations. Commented Mar 28, 2011 at 23:45

2 Answers 2

3

I'd be more defensive and write it this way:

public void list(File file) 
{
    if (file != null && file.isDirectory())
    {
       File[] children = file.listFiles();
       if (children != null)
       {
           for(int i = 0; i < children.length; i++)
           {
               if (children[i] != null)
               {
                   Log.d(TAG, children[i].getName());
               }
               list(children[i]);
           }
        }
    }
}

Interesting: Is that Log class yours, or is that a logging solution that's commonly used on Android?

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

2 Comments

Very commonly used. Details here.
A bit of experimentation reveals that there is indeed a hidden object on the SD card root (.android_security) which returns true for isDirectory() but barfs all over the place when you call .listFiles() on it. I think I'll probably conditionalise it to screen for both null objects and hidden ones. Thanks.
1

You could be getting null pointer exceptions because the SD card is unavailable (maybe because your phone is connected to your pc while debugging?)

I've had a similar error before and that was the reason why.

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.