2014-01-21 44 views
0

我有一個n-triple文件,我需要通過Jena SDB模型將它存儲在數據庫中。我寫了下面的代碼來做到這一點。但是我對一個java.lang.NullPointerException異常掙扎,我並不知道它是如何發生的以及如何解決它。 下面是代碼:我在將三重文件加載到Jena SDB模型時遇到java.lang.NullPointerException

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) 

最後這裏時的輸出在n三重文件的樣本

<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> . 

提前致謝。

回答

2

基於此代碼(我猜就行號,但我敢肯定我是對的):

/* 69 */ InputStream in = this.getClass().getClassLoader().getResourceAsStream("data/nts/100.nt"); 
/* 70 */ traceModel.read(in, xmlbase, "N-TRIPLE"); 

和堆棧跟蹤的開頭:

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) 
    … 

我猜innull。也許這個路徑名必須是"/data/nts/100.nt"(注意開頭的/)。

stacktrace包含行號,我很確定他們指出你有問題的一行。這在調試器中不應該太難運行。即使你不能在調試器中運行它,如果你得到一個空指針異常,並且你有一個特定行的引用,通常是檢查該行上使用哪些值並檢查它們是否是一個好主意是不是null。在這種情況下,你可以這樣做:

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"); 
+0

@FopaLéonConstantin如果文檔說它是「N-TRIPLE」,它可能是「N-TRIPLE」。我沒有在發佈前檢查文檔;這僅僅是第二次猜測,如果第一次沒有解決NPE可能來自哪裏。既然它可能會讓讀者感到困惑,我會將其從我的答案中刪除。 –

相關問題