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?
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 throwjava.io.IOExceptiondespite the fact that other methods on the class do in similar situations.