Bogus NullPointerExceptions
while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
== 0) {
File parent = dir.getParentFile();
dir.delete();
dir = parent;
}
is being used to nuke some empty directories in an app of mine, then
the parent if it's now empty, and so forth up the chain to a top
directory.
Sometimes, the while line is throwing an NPE, a problem that seems
impossible.
First, baseDir is not null. It's set only once and never changed,
nothing that uses it ever throws an NPE except this line, and this line
only uses it as the RHS of .equals(), where null is supposed to be
legal anyway.
Second, dir is not null (I added an explicit throw of NPE if dir was
null just before the "while" loop and the "while" line still threw the
NPEs, rather than the line with the explicit throw).
Finally, dir.list().length is accessed only after a short-circuit and
after a test that dir.list() isn't null.
The only logical explanation seems to be that dir.list() can return an
array one nanosecond and null the next...
I don't suppose this is some weird concurrency problem. I guess I'll
try synchronizing on something (baseDir?) before the loop...