how to run TraversalExample.java example

34 views
Skip to first unread message

Tingting Cui

unread,
Apr 17, 2014, 3:11:32 AM4/17/14
to ne...@googlegroups.com
when i try to run TraversalExample.java example and create the graph like this 
it give me error like this
Exception in thread "main" java.lang.NullPointerException
at setupneo4j.TraversalExample.<init>(TraversalExample.java:23)
at setupneo4j.TraversalExample.main(TraversalExample.java:33)
what is the problems here.
public static void main( String[] args )
    {
        
    TraversalExample op = new TraversalExample( db );
        op.shutdownGraph();
    }
    public void shutdownGraph()
    {
        try
        {
            if ( db != null )
            {
                db.shutdown();
            }
        }
        finally
        {
            db = null;
        }
    }

    public Node createTheGraph()
    {
        try ( Transaction tx = db.beginTx() )
        {
            // START SNIPPET: createGraph
            Node A = db.createNode();
            Node B = db.createNode();
            Node C = db.createNode();
            Node D = db.createNode();
    
            A.createRelationshipTo( C, Rels.KNOWS );
            C.createRelationshipTo( D, Rels.KNOWS );
            A.createRelationshipTo( B, Rels.KNOWS);
            B.createRelationshipTo( C, Rels.KNOWS );
            // END SNIPPET: createGraph
            A.setProperty( "name", "A" );
            B.setProperty( "name", "B" );
            C.setProperty( "name", "C" );
            D.setProperty( "name", "D" );
            tx.success();
            return A;
        }
    }

Anders Nawroth

unread,
Apr 17, 2014, 6:56:44 AM4/17/14
to ne...@googlegroups.com
Hi!

Did you initialize the db field?

Something like 
db  = new GraphDatabaseFactory().newEmbeddedDatabase( "path/to/db" );

/anders


--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Anders Nawroth [and...@neotechnology.com]
Skype: anders.nawroth
Message has been deleted

TC

unread,
Apr 17, 2014, 9:21:47 PM4/17/14
to ne...@googlegroups.com, and...@neotechnology.com
why cannot i use this way to create node use int array
                int[] newnode=new int[10];
        for(int i=0; i<newnode.length; i++){
        Node newnode[i]=db.createNode();
        }
       
        newnode[1].createRelationshipTo(newnode[2], Rels.KNOWS );

Michael Hunger

unread,
Apr 18, 2014, 12:44:03 AM4/18/14
to ne...@googlegroups.com
You should use a Node[] array

Perhaps you want to rather use cypher from another language than writing Java code?

Sent from mobile device

TC

unread,
Apr 18, 2014, 9:06:31 AM4/18/14
to ne...@googlegroups.com
hi, i use node array. i think it is fine now. but i have another question here.
public static void main( String[] args )
    {
    final String DB_PATH = "target/neo4j-hello-db";
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
    TraversalExample op = new TraversalExample( db );
    op.createTheGraph();
        op.shutdownGraph();
    }
    public void shutdownGraph()
    {
        try
        {
            if ( db != null )
            {
                db.shutdown();
            }
        }
        finally
        {
            db = null;
        }
    }

    public Node createTheGraph()
    {
        try ( Transaction tx = db.beginTx() )
        {
            // START SNIPPET: createGraph
       
        Node[] newno=new Node[5];
        for(int i=0; i<newno.length; i++){
        newno[i]=db.createNode();
        }
       
        newno[1].createRelationshipTo(newno[2], Rels.KNOWS );
        newno[1].createRelationshipTo(newno[4], Rels.KNOWS );
        newno[4].createRelationshipTo(newno[2], Rels.KNOWS );
        newno[2].createRelationshipTo(newno[3], Rels.KNOWS );
        System.out.println(knowsLikesTraverser(newno[1]));
       
            return newno[1];
        }
    }
    public String knowsLikesTraverser( Node node )
    {
        String output = "";
        // START SNIPPET: knowslikestraverser
        for ( Path position : db.traversalDescription()
                .depthFirst()
                .relationships( Rels.KNOWS )
                
                .evaluator( Evaluators.toDepth( 5 ) )
                .traverse( node ) )
        {
            output += position + "\n";
        }
        // END SNIPPET: knowslikestraverser
        return output;
    }

while it give me output like this.
(4350)
(4350)--[KNOWS,8328]-->(4351)
(4350)--[KNOWS,8328]-->(4351)--[KNOWS,8331]-->(4352)
(4350)--[KNOWS,8328]-->(4351)<--[KNOWS,8330]--(4353)

why it give the number like 4350 and 8328? this big number. it supposed to be just 0-4. Thanks.
if i just want to node to traversal forward and backward separately.  Right now the traversal is mixed together.
Thanks.
Reply all
Reply to author
Forward
0 new messages