Re: Bogus NullPointerExceptions
Twisted wrote:
while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
== 0) {
File parent = dir.getParentFile();
dir.delete();
dir = parent;
}
There is one big problem with your code. dir can be changed during the
loop, so it does not matter whether you are testing dir for null before
the loop starts. According to the code you have shown us, baseDir only
needs to be checked before the loop. because its not used elsewhere in
the code so it does not change.
The reason for your problem is that you can *not guarantee* that any of
the references/methods you are using does not return null (you are
experiencing an NPE even when you claim there can not be any).
In addition, your code is very unstable. If, for a reason, you change
how baseDir is used or how getParentFile() works, you could get into
problems again in the future.
What you need to do is the following, for absolute certainty:
while (dir != null && baseDir != null &&
!dir.equals(baseDir) &&
dir.list() != null &&
dir.list().length > == 0) {
...
}
tom