Search code examples
sparqljenavirtuosoopenlink-virtuoso

Null pointer exception error while using BIND in SPARQL query in JENA virtuoso


I am getting below error while running SPARQL query,

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)

Sample TTL file data

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

I am using the Virtuoso SPARQL endpoint to run a query

here is my code which i am running.

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

<dependency>
    <groupId>com.hp.hpl.jena</groupId>
    <artifactId>arq</artifactId>
    <version>2.8.8</version>
</dependency>

I am facing strange issue while using BIND in my SPARQL query. I don't understand what's the actual problem or I can't figure out why this is happening. Also, I am using the Virtuoso for graph data store and I am using Jena lib for running the SPARQL query. While running the query I am getting error.I didn't find any solution on google.


Solution

  • As revealed in comments by @AKSW, answer is to

    1. update to current Jena (and possibly current Virtuoso server [whether Enterprise or Open Source], Virtuoso Provider for Jena, and/or Virtuoso JDBC driver)
    2. use correct call/query pairing (i.e., call Jena .execSelect() with a SPARQL SELECT query, or call Jena .execDescribe() with a SPARQL DESCRIBE query).