在 JENA virtuoso 的 SPARQL 查询中使用 BIND 时出现空指针异常错误
Null pointer exception error while using BIND in SPARQL query in JENA virtuoso
我在 运行 宁 SPARQL 查询时出现以下错误,
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 7): {E201} The attributes on this property element, are not permitted with any content; expecting end element tag.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 38): {E201} XML element <res:binding> inside an empty property element, whose attributes prohibit any content.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 52): {E201} XML element <res:variable> inside an empty property element, whose attributes prohibit any content.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 57): {E201} The attributes on this property element, are not permitted with any content; expecting end element tag.
WARN [main] (RDFDefaultErrorHandler.java:36) - (line 8 column 135): {W102} unqualified use of rdf:datatype is deprecated.
java.lang.NullPointerException
at com.hp.hpl.jena.rdf.arp.impl.XMLHandler.endElement(XMLHandler.java:149)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNamespaceBinder.handleEndElement(Unknown Source)
at org.apache.xerces.impl.XMLNamespaceBinder.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.parse(RDFXMLParser.java:142)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:158)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:145)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:215)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:197)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execModel(QueryEngineHTTP.java:169)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execDescribe(QueryEngineHTTP.java:162)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execDescribe(QueryEngineHTTP.java:160)
at com.hospital.SPARQLQuery.main(SPARQLQuery.java:34)
示例 TTL 文件数据
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book1 ns:discount 0.2 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
:book2 ns:discount 0.25 .
我正在使用 Virtuoso SPARQL 端点 运行 查询
这是我的代码,我是 运行ning。
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
public class SPARQLQuery {
private static final Logger logger = LogManager.getLogger(SPARQLQuery.class);
public static void main(String[] args) {
try {
String queryString = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" +
"PREFIX ns: <http://example.org/ns#>\r\n" +
"\r\n" +
"SELECT ?title ?price\r\n" +
"{ ?x ns:price ?p .\r\n" +
" ?x ns:discount ?discount\r\n" +
" BIND (?p*(1-?discount) AS ?price)\r\n" +
" FILTER(?price < 20)\r\n" +
" ?x dc:title ?title . \r\n" +
"}";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
Maven 依赖
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
我在 SPARQL 查询中使用 BIND 时遇到奇怪的问题。我不明白实际问题是什么,或者我不明白为什么会这样。此外,我将 Virtuoso 用于图形数据存储,并将 Jena 库用于 运行ning SPARQL 查询。虽然 运行 查询我正在 error.I 没有在 google.
上找到任何解决方案
正如@AKSW 在评论中所揭示的那样,答案是
- 更新为 current Jena (and possibly current Virtuoso server [whether Enterprise or Open Source], Virtuoso Provider for Jena, and/or Virtuoso JDBC driver)
- 使用正确的 call/query 配对(即,使用 SPARQL
SELECT
查询调用 Jena .execSelect()
,或使用 SPARQL DESCRIBE
查询调用 Jena .execDescribe()
).
我在 运行 宁 SPARQL 查询时出现以下错误,
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 7): {E201} The attributes on this property element, are not permitted with any content; expecting end element tag.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 38): {E201} XML element <res:binding> inside an empty property element, whose attributes prohibit any content.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 52): {E201} XML element <res:variable> inside an empty property element, whose attributes prohibit any content.
ERROR [main] (RDFDefaultErrorHandler.java:40) - (line 7 column 57): {E201} The attributes on this property element, are not permitted with any content; expecting end element tag.
WARN [main] (RDFDefaultErrorHandler.java:36) - (line 8 column 135): {W102} unqualified use of rdf:datatype is deprecated.
java.lang.NullPointerException
at com.hp.hpl.jena.rdf.arp.impl.XMLHandler.endElement(XMLHandler.java:149)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNamespaceBinder.handleEndElement(Unknown Source)
at org.apache.xerces.impl.XMLNamespaceBinder.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.hp.hpl.jena.rdf.arp.impl.RDFXMLParser.parse(RDFXMLParser.java:142)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:158)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:145)
at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:215)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:197)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execModel(QueryEngineHTTP.java:169)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execDescribe(QueryEngineHTTP.java:162)
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execDescribe(QueryEngineHTTP.java:160)
at com.hospital.SPARQLQuery.main(SPARQLQuery.java:34)
示例 TTL 文件数据
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix : <http://example.org/book/> .
@prefix ns: <http://example.org/ns#> .
:book1 dc:title "SPARQL Tutorial" .
:book1 ns:price 42 .
:book1 ns:discount 0.2 .
:book2 dc:title "The Semantic Web" .
:book2 ns:price 23 .
:book2 ns:discount 0.25 .
我正在使用 Virtuoso SPARQL 端点 运行 查询
这是我的代码,我是 运行ning。
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
public class SPARQLQuery {
private static final Logger logger = LogManager.getLogger(SPARQLQuery.class);
public static void main(String[] args) {
try {
String queryString = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\r\n" +
"PREFIX ns: <http://example.org/ns#>\r\n" +
"\r\n" +
"SELECT ?title ?price\r\n" +
"{ ?x ns:price ?p .\r\n" +
" ?x ns:discount ?discount\r\n" +
" BIND (?p*(1-?discount) AS ?price)\r\n" +
" FILTER(?price < 20)\r\n" +
" ?x dc:title ?title . \r\n" +
"}";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
Maven 依赖
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
我在 SPARQL 查询中使用 BIND 时遇到奇怪的问题。我不明白实际问题是什么,或者我不明白为什么会这样。此外,我将 Virtuoso 用于图形数据存储,并将 Jena 库用于 运行ning SPARQL 查询。虽然 运行 查询我正在 error.I 没有在 google.
上找到任何解决方案正如@AKSW 在评论中所揭示的那样,答案是
- 更新为 current Jena (and possibly current Virtuoso server [whether Enterprise or Open Source], Virtuoso Provider for Jena, and/or Virtuoso JDBC driver)
- 使用正确的 call/query 配对(即,使用 SPARQL
SELECT
查询调用 Jena.execSelect()
,或使用 SPARQLDESCRIBE
查询调用 Jena.execDescribe()
).