Mysql 我在将n-triple文件加载到Jena SDB模型时遇到java.lang.NullPointerException问题
我有一个n-triple文件,我需要通过Jena SDB模型将其存储在数据库中。为此,我编写了以下代码。但是我遇到了一个java.lang.NullPointerException,我真的不知道是什么导致了它,以及如何修复它。 代码如下:Mysql 我在将n-triple文件加载到Jena SDB模型时遇到java.lang.NullPointerException问题,mysql,nullpointerexception,jena,n-triples,Mysql,Nullpointerexception,Jena,N Triples,我有一个n-triple文件,我需要通过Jena SDB模型将其存储在数据库中。为此,我编写了以下代码。但是我遇到了一个java.lang.NullPointerException,我真的不知道是什么导致了它,以及如何修复它。 代码如下: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import com.hp.hpl.jena.db.DBCon
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.ProfileRegistry;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
public class JenaRepositoryManagement {
private MySQLAccess mysql = null;
private String dbname = null;
private String xmlbase = "http://www.test.org/test.owl#";
private OntModel domainModel = null;
public JenaRepositoryManagement (String databaseName) {
dbname = "sdb_" + databaseName;
mysql = new MySQLAccess();
mysql.createDatabase(dbname);
impotring();
}
public void importing() {
// Instantiate the MySQL driver
try {
Class.forName("com.mysql.jdbc.Driver");
// Create a database connection object
DBConnection connection = new DBConnection("jdbc:mysql://localhost/"+dbname, "root", "", "MySQL");
// Get a ModelMaker for database-backed models
ModelMaker maker = ModelFactory.createModelRDBMaker(connection);
Model traceModel = maker.createModel("testModel");
// Start a database transaction. Without one, each statement will be auto-committed
traceModel.begin();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("data/nts/100.nt");
traceModel.read(in, xmlbase, "N-TRIPLE");
traceModel.write(System.out);
// Commit the database transaction
traceModel.commit();
} catch (ClassNotFoundException | FileNotFoundException e) {
e.printStackTrace();
}
}
}
这是执行期间的输出
CREATE DATABASE IF NOT EXISTS sdb_100_n;
java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:129)
at com.hp.hpl.jena.util.FileUtils.asUTF8(FileUtils.java:51)
at com.hp.hpl.jena.rdf.model.impl.NTripleReader.read(NTripleReader.java:72)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:226)
at soctrace.JenaRepositoryManagement.importing(JenaRepositoryManagement.java:70)
at soctrace.views.Main$1.run(Main.java:165)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java .security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
创建数据库(如果不存在sdb_100_n);
java.lang.NullPointerException
在java.io.Reader上。(Reader.java:78)
位于java.io.InputStreamReader。(InputStreamReader.java:129)
位于com.hp.hpl.jena.util.FileUtils.asUTF8(FileUtils.java:51)
在com.hp.hpl.jena.rdf.model.impl.NTripleReader.read(NTripleReader.java:72)上
位于com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:226)
在soctrace.jenarespositorymanagement.importing(jenarespositorymanagement.java:70)
位于soctrace.views.Main$1.run(Main.java:165)
在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)中
位于java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
在java.awt.EventQueue.access$200(EventQueue.java:103)
在java.awt.EventQueue$3.run(EventQueue.java:688)
在java.awt.EventQueue$3.run(EventQueue.java:686)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
位于java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
在java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread.java:161)
位于java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThread.java:150)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
位于java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
最后,这里是一个n-triple文件的示例
<http://www.test.org/test.owl#trace0> <http://www.test.org/test.owl#traceContainsEvent> <http://www.test.org/test.owl#event0> .
<http://www.test.org/test.owl#event0> <http://www.test.org/test.owl#hasTime> <http://www.test.org/test.owl#interval0> .
<http://www.test.org/test.owl#event0> <http://www.test.org/test.owl#hasActiveTime> <http://www.test.org/test.owl#aduration0> .
<http://www.test.org/test.owl#interval0> <http://www.test.org/test.owl#hasStartTime> <http://www.test.org/test.owl#stime0> .
<http://www.test.org/test.owl#interval0> <http://www.test.org/test.owl#hasEndTime> <http://www.test.org/test.owl#etime0> .
<http://www.test.org/test.owl#interval0> <http://www.test.org/test.owl#hasDuration> <http://www.test.org/test.owl#duration0> .
<http://www.test.org/test.owl#stime0> <http://www.test.org/test.owl#hasValue> "525255"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#etime0> <http://www.test.org/test.owl#hasValue> "0"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#duration0> <http://www.test.org/test.owl#hasValue> "0"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#aduration0> <http://www.test.org/test.owl#hasValue> "0"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#event0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.test.org/test.owl#ContextSwitch> .
<http://www.test.org/test.owl#event0> <http://www.test.org/test.owl#switchFrom> <http://www.test.org/test.owl#mttd0> .
<http://www.test.org/test.owl#event0> <http://www.test.org/test.owl#switchTo> <http://www.test.org/test.owl#kptrace0> .
<http://www.test.org/test.owl#trace0> <http://www.test.org/test.owl#traceContainsEvent> <http://www.test.org/test.owl#event1> .
<http://www.test.org/test.owl#event1> <http://www.test.org/test.owl#hasTime> <http://www.test.org/test.owl#interval1> .
<http://www.test.org/test.owl#event1> <http://www.test.org/test.owl#hasActiveTime> <http://www.test.org/test.owl#aduration1> .
<http://www.test.org/test.owl#interval1> <http://www.test.org/test.owl#hasStartTime> <http://www.test.org/test.owl#stime1> .
<http://www.test.org/test.owl#interval1> <http://www.test.org/test.owl#hasEndTime> <http://www.test.org/test.owl#etime1> .
<http://www.test.org/test.owl#interval1> <http://www.test.org/test.owl#hasDuration> <http://www.test.org/test.owl#duration1> .
<http://www.test.org/test.owl#stime1> <http://www.test.org/test.owl#hasValue> "525292"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#etime1> <http://www.test.org/test.owl#hasValue> "525306"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#duration1> <http://www.test.org/test.owl#hasValue> "14"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#aduration1> <http://www.test.org/test.owl#hasValue> "14"^^<http://www.w3.org/2001/XMLSchema#float> .
<http://www.test.org/test.owl#event1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.test.org/test.owl#TaskRunning> .
<http://www.test.org/test.owl#event1> <http://www.test.org/test.owl#runningTask> <http://www.test.org/test.owl#kptrace0> .
。
.
.
.
.
.
"525255"^^ .
"0"^^ .
"0"^^ .
"0"^^ .
.
.
.
.
.
.
.
.
.
"525292"^^ .
"525306"^^ .
"14"^^ .
"14"^^ .
.
.
提前感谢。基于此代码(我在猜行号,但我很确定我是对的):
/*69*/InputStream in=this.getClass().getClassLoader().getResourceAsStream(“data/nts/100.nt”);
/*70*/traceModel.read(in,xmlbase,“N-TRIPLE”);
以及堆栈跟踪的开始:
java.lang.NullPointerException
在java.io.Reader上。(Reader.java:78)
位于java.io.InputStreamReader。(InputStreamReader.java:129)
位于com.hp.hpl.jena.util.FileUtils.asUTF8(FileUtils.java:51)
在com.hp.hpl.jena.rdf.model.impl.NTripleReader.read(NTripleReader.java:72)上
位于com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:226)
…
我猜中的是null
。可能路径名需要是“/data/nts/100.nt”
(注意开头的/
)
stacktrace包含行号,我很确定它们会指向有问题的行。这在调试器中运行应该不会太难。即使您不能在调试器中运行它,如果您得到一个空指针异常,并且您有一个对特定行的引用,通常最好检查该行上使用了哪些值,并检查它们是否为空。在这种情况下,您可以执行以下操作:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("data/nts/100.nt");
Objects.requireNonNull( in, "InputStream for resource data/nts/100.nt should not be null." );
traceModel.read(in, xmlbase, "N-TRIPLE");
@FopaLéonConstantin如果医生说它是“N-TRIPLE”,那么它可能是“N-TRIPLE”。我在发布之前没有检查文档;这只是一个次要的猜测,如果第一个不起作用,NPE可能来自哪里。因为这可能会让发现这一点的读者感到困惑,所以我将把它从我的答案中删除。