我想通过java驱动程序在centOS远程服务器上连接我的neo4j数据库。我尝试创建neo4j traverse文件夹并将每个文件插入数据库。代码如下所示:
package neo;
import org.neo4j.driver.v1.*;
import java.io.File;
public class HW {
Driver driver;
public HW(String uri, String user, String password) {
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
}
private void addCompany(String name) {
// Sessions are lightweight and disposable connection wrappers.
try (Session session = driver.session()) {
// Wrapping Cypher in an explicit transaction provides atomicity
// and makes handling errors much easier.
try (Transaction tx = session.beginTransaction()) {
tx.run("call apoc.load.json(\"file:///home//ddd//data//shanghai//"+name+"\")yield value as company " +
"create(c:company) " +
"with c,company " +
"unwind company.organizationInformationList as a " +
"set c = a "
);
tx.success();
}
}
}
public void close() {
// Closing a driver immediately shuts down all open connections.
driver.close();
}
public static void main(String... args) {
File file = new File("/home/ddd/data/shanghai");
File next[] = file.listFiles();
HW example = new HW("bolt://202.112.113.71:7687", "neo4j", "rucer2017");
String CompanyName = "";
for (int i = 0; i < next.length; i++) {
CompanyName=next[i].getName();
example.addCompany(CompanyName);
System.out.println(CompanyName);
}
example.close();
}
}
代码无法运行。错误是:
org.neo4j.driver.internal.logging.JULogger info
信息: Driver instance org.neo4j.driver.internal.InternalDriver@23ceabc1 created
Exception in thread "main" java.lang.NullPointerException
at neo.HW.main(HW.java:129)
我花了很多时间修复它并失败了。此外,代码适用于我的本地Windows版Neo4j Desktop.Could任何人都可以帮助我?非常感谢。
答案 0 :(得分:0)
它看起来像&#34; / home / ddd / data / shanghai&#34;不是运行此代码的计算机上的目录的路径。如果该目录不存在,则file.listFiles()
将返回null
,并且取消引用next.length
的尝试将引发NullPointerException
。