[Neo] relationship.getOtherNode(localnode) returning null?

Emil Eifrem emil at neopersistence.com
Wed Jun 27 14:18:39 CEST 2007


On 6/27/07, Peter Neubauer <peter.neubauer at jayway.se> wrote:
> Well,
> two problems here. First the code givign me null is this:
> public int cmdLs(Dictionary opts, Reader in, PrintWriter out,
>             Session session) {
>         TransactionUtil.beginTx();

Mostly for the archives: this is deprecated APIs. TransactionUtil is
in org.neo4j.impl and should thus never be used by client
applications. Proper use of the API would be:

Transaction tx = Transaction.begin();
try
{
... lots of code ..
   tx.success();
}
finally
{
   tx.finish();
}

This is more robust, convenient and uses only classes from org.neo4j.api.

But that aside, on to your real problems...

>         Node localnode = currentNode;
>         String[] ids = (String[]) opts.get("id");
>         if(ids.length == 1)
>         {
>                 localnode = neo.getNodeById(Long.parseLong(ids[0]));
>         }
>         Iterable<Relationship> relationships =
> localnode.getRelationships(Direction.OUTGOING);
>         for (Relationship relationship : relationships) {
>                         out.println(localnode.getId() + "--->" +
> nodeToString(relationship.getOtherNode(localnode)));
>                 }
>         Iterable<Relationship> incomingRelationships =
> currentNode.getRelationships(Direction.OUTGOING);

First, note that incomingRelationships are from the "currentNode"
variable. Second, and as Johan pointed out, there's probably a typo
here (Direction.OUTGOING).

>         for (Relationship relationship : incomingRelationships) {
>                         out.println(localnode.getId() + "<---" +
> nodeToString(relationship.getOtherNode(localnode)));

Here you pass potentially another node (though they may at times be
aliased to the same object) to the getOtherNode() method. If
"localNode" is not attached to either end of "relationship," you
should get a runtime exception, though with the current implementation
you get null. (This API conformance bug has been fixed in b6-SNAPSHOT
which is mvn deployed to m2.neo4j.org regularly, so you can upgrade if
you want to.)

>                 }
>         TransactionUtil.finishTx(true, true);
>         return 0;
>     }
>
> Throwing a null into nodeToString(Node).
>
> Secondly, Direction.OUTGOING gives med the same nodes as
> Direction.INCOMING. Is that intended and is it relevant at all, in
> that case, should it be removed?

No, this is probably because of your typo.

-- 
Emil Eifrém, CEO [emil at neopersistence.com]
Neo Persistence AB, www.neopersistence.com
Cell: +46 733 462 271 | US Cell: 206 403 8808


More information about the User mailing list