You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by "McDonnell, David" <Da...@csu.edu.au> on 2001/02/09 04:04:58 UTC

NullPointerException on getParent

Hello all

I'm getting a NullPointerException on this call:

structure.getParent(token, currentNode);

This only occurs when currentNode is the root node, ie "/". (and therefore
it doesnt have a parent).

I traced it to this line of code. getParentUri is returning null, and
toString is causing the NullPointerException. 

        String parentUri = namespace.getUri(objectUri)
            .getParentUri().toString();

It would be nice if either Uri.getParentUri or Structure.getParent could
throw a NodeHasNoParentException or something similar if it is dealing with
the root node.

Cheers,
Dave

Re: NullPointerException on getParent

Posted by Remy Maucherat <re...@apache.org>.
> Hello all
>
> I'm getting a NullPointerException on this call:
>
> structure.getParent(token, currentNode);
>
> This only occurs when currentNode is the root node, ie "/". (and therefore
> it doesnt have a parent).
>
> I traced it to this line of code. getParentUri is returning null, and
> toString is causing the NullPointerException.
>
>         String parentUri = namespace.getUri(objectUri)
>             .getParentUri().toString();
>
> It would be nice if either Uri.getParentUri or Structure.getParent could
> throw a NodeHasNoParentException or something similar if it is dealing
with
> the root node.

I feel like returning null in such a case.
When using that call in a loop, it's easier to deal with.

ObjectNode node = retrieveSomeNode();
do {
    // do something

    node = structure.getParent(token, node);
} while (node != null);

Remy