Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Bogus NullPointerExceptions

Reply
Thread Tools

Bogus NullPointerExceptions

 
 
Twisted
Guest
Posts: n/a
 
      11-15-2006
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...

 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-15-2006
Twisted wrote:
> 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...
>


Additionally, your code seems to me to depend on baseDir being on the
parent chain from the initial value of dir.

If that were not the case, you would reach the end of the parent chain
without encountering the stop condition, so getParentFile would return
null. If that happened, dir would be null in the while test without
having been null when you went through the check before the while loop.

If you have not already done so, perhaps check the code that calculates
dir and baseDir?

Patricia
 
Reply With Quote
 
 
 
 
Twisted
Guest
Posts: n/a
 
      11-15-2006
Patricia Shanahan wrote:
> Additionally, your code seems to me to depend on baseDir being on the
> parent chain from the initial value of dir.


It does, and earlier code guarantees that baseDir is on the parent
chain. (Certain dirs under baseDir are checked for being empty and
deleted if so.)

 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      11-15-2006

"Twisted" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
> == 0) {
> File parent = dir.getParentFile();


Note that getParentFile() can return null.

> dir.delete();
> dir = parent;


if parent is null, dir is now null, and dir.equals() will generate a NPE

> }
>
><snip>
>
> Sometimes, the while line is throwing an NPE, a problem that seems
> impossible.
>

<snip>
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-15-2006
Twisted wrote:
> Patricia Shanahan wrote:
>> Additionally, your code seems to me to depend on baseDir being on the
>> parent chain from the initial value of dir.

>
> It does, and earlier code guarantees that baseDir is on the parent
> chain. (Certain dirs under baseDir are checked for being empty and
> deleted if so.)
>


The fact that directory X is under directory Y does not guarantee that
all File objects for X have Y as a getParentFile ancestor.

Of course, the way you are creating the File object may ensure that dir
does have baseDir as a getParentFile ancestor, but if I were you I would
put in a check for null getParentFile result just in case.

Patricia
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-16-2006
Fred Kleinschmidt wrote:
> "Twisted" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...
> > while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
> > == 0) {
> > File parent = dir.getParentFile();

>
> Note that getParentFile() can return null.


I should have been clearer in the original post. This code is reached
only with dir a descendant of baseDir, so it can't reach the root and
try to keep going.

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-16-2006
Patricia Shanahan wrote:
> The fact that directory X is under directory Y does not guarantee that
> all File objects for X have Y as a getParentFile ancestor.


It should, as long as X is genuinely under Y rather than some kind of
shortcut to X being under Y. In this particular application, is
certainly is under Y (it was reached by traversal from Y to begin
with).

Anyway it does seem to have been a concurrency issue -- synchronizing
on baseDir (which is app global and unchanging) at several key spots in
the code made the NPEs go away.

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-16-2006

Twisted wrote:
> Fred Kleinschmidt wrote:
> > "Twisted" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ps.com...
> > > while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
> > > == 0) {
> > > File parent = dir.getParentFile();

> >
> > Note that getParentFile() can return null.

>
> I should have been clearer in the original post. This code is reached
> only with dir a descendant of baseDir, so it can't reach the root and
> try to keep going.


It doesn't hurt to add an assert.

File parent = dir.getParentFile();
assert parent != null : "Whoops, missed my parent!";

 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      11-16-2006
Twisted <(E-Mail Removed)> wrote:
> Anyway it does seem to have been a concurrency issue -- synchronizing
> on baseDir (which is app global and unchanging) at several key spots in
> the code made the NPEs go away.


There are two global (principially) modifyable "objects" involved:
baseDir, which you're sure remains unmodified throughout the
running program, and the filesystem itself!

Perhaps the method gets to run twice in parallel, and
the parent-directory obtained in first thread has already
been deleted in the other thread just before you get
to deal with it in the first one. Or something like that.

In that case, synchronizing on baseDir obviously solved
the problem, even if just indirectly. Probably it would
have been enough to synchronize only those methods on baseDir
that actually modify the filesystem's subtree starting at
baseDir.

 
Reply With Quote
 
Ingo Menger
Guest
Posts: n/a
 
      11-16-2006

Twisted schrieb:

> Fred Kleinschmidt wrote:
> > "Twisted" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ps.com...
> > > while (!dir.equals(baseDir) && dir.list() != null && dir.list().length
> > > == 0) {
> > > File parent = dir.getParentFile();

> >
> > Note that getParentFile() can return null.

>
> I should have been clearer in the original post. This code is reached
> only with dir a descendant of baseDir, so it can't reach the root and
> try to keep going.


The NullPointerException tells you, however, that it can or that there
may be other reasons why getParentFile() returns null.
BTW, make sure you write code that will work with symbolic links.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList and recieving nullPointerExceptions. Mitch Java 13 03-10-2006 01:18 PM
NTP on a router picking up a bogus server John Caruso Cisco 5 11-26-2005 04:58 PM
Would like examples of NullPointerExceptions for tool testing Alistair Hutton Java 4 06-18-2004 10:12 PM
Filtering bogus TCP packets David Cisco 5 06-03-2004 12:45 PM
Rogue NullPointerExceptions in IBM JDK 1.4.0 James Java 0 02-13-2004 05:34 PM



Advertisments