我尝试运行TraversalExample.java
并在课程中创建图表
但我得到以下错误
Exception in thread "main" java.lang.NullPointerException
at setupneo4j.TraversalExample.<init>(TraversalExample.java:23)
at setupneo4j.TraversalExample.main(TraversalExample.java:33)
这里有什么问题?
我试图在Neo4j的上一篇文章中创建图表
但它似乎失败了。
package setupneo4j;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Uniqueness;
public class TraversalExample
{
private static GraphDatabaseService db;
private TraversalDescription friendsTraversal;
public TraversalExample( GraphDatabaseService db )
{
this.db = db;
// START SNIPPET: basetraverser
friendsTraversal = db.traversalDescription()
.depthFirst()
.relationships( Rels.KNOWS )
.uniqueness( Uniqueness.RELATIONSHIP_GLOBAL );
// END SNIPPET: basetraverser
}
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;
}
}
public String knowsLikesTraverser( Node node )
{
String output = "";
// START SNIPPET: knowslikestraverser
for ( Path position : db.traversalDescription()
.depthFirst()
.relationships( Rels.KNOWS )
.relationships( Rels.LIKES, Direction.INCOMING )
.evaluator( Evaluators.toDepth( 5 ) )
.traverse( node ) )
{
output += position + "\n";
}
// END SNIPPET: knowslikestraverser
return output;
}
public String traverseBaseTraverser( Node node )
{
String output = "";
// START SNIPPET: traversebasetraverser
for ( Path path : friendsTraversal.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: traversebasetraverser
return output;
}
public String depth3( Node node )
{
String output = "";
// START SNIPPET: depth3
for ( Path path : friendsTraversal
.evaluator( Evaluators.toDepth( 3 ) )
.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: depth3
return output;
}
public String depth4( Node node )
{
String output = "";
// START SNIPPET: depth4
for ( Path path : friendsTraversal
.evaluator( Evaluators.fromDepth( 2 ) )
.evaluator( Evaluators.toDepth( 4 ) )
.traverse( node ) )
{
output += path + "\n";
}
// END SNIPPET: depth4
return output;
}
public String nodes( Node node )
{
String output = "";
// START SNIPPET: nodes
for ( Node currentNode : friendsTraversal
.traverse( node )
.nodes() )
{
output += currentNode.getProperty( "name" ) + "\n";
}
// END SNIPPET: nodes
return output;
}
public String relationships( Node node )
{
String output = "";
// START SNIPPET: relationships
for ( Relationship relationship : friendsTraversal
.traverse( node )
.relationships() )
{
output += relationship.getType().name() + "\n";
}
// END SNIPPET: relationships
return output;
}
// START SNIPPET: sourceRels
private enum Rels implements RelationshipType
{
LIKES, KNOWS
}
// END SNIPPET: sourceRels
}
答案 0 :(得分:0)
您永远不会实际创建数据库,只需将该字段声明为类属性,将其传递给构造函数,然后将其设置为自身。当你调用它时null
,你得到一个NullPointerException
。我不知道你从哪里得到代码,但你应该从改变
private static GraphDatabaseService db;
到
private GraphDatabaseService db;
然后在main
中初始化db,然后再将其传递给构造函数,类似于
final String DB_PATH = "path/to/your/db"
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);