javanullpointerexceptionjenakaraf

How to debug NullPointerException at apache.jena.queryExecutionFactory during create?


I am working with org.apache.jena with karaf. While trying to query my model I get a NullPointerException I printed all variables I use for creating the query and all(queryString, inferedModel) are not null. Here is what my code looks like :

Model model = JenaEngine.readModel(inputDataOntology);
if (model == null) {
    return "model null !";
}
Model inferedModel = JenaEngine.readInferencedModelFromRuleFile(model, inputRule);
if (inferedModel == null) {
    return "inferedModel null !";
}
JenaEngine.updateValueOfDataTypeProperty(inferedModel, ns, "Peter", "age", 10);
JenaEngine.updateValueOfObjectProperty(inferedModel, ns, "Peter", "estFilsDe", "Femme1");
JenaEngine.createInstanceOfClass(model, ns, "Femme", "abc");
//query on the model
String prefixQuery = "PREFIX ns: <http://www.owl-ontologies.com/Ontology1291196007.owl#>\n" +
    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
    "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"+
    "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n";
String selectQuery = "SELECT ?user ?age WHERE {\n ?user ns:age ?age.\n }";
QueryExecution queryExecution = QueryExecutionFactory.create(prefixQuery+selectQuery, inferedModel);
ResultSet rs = queryExecution.execSelect();
List<String> users = new ArrayList();
while(rs.hasNext()) {
    QuerySolution qs = rs.next();
    Resource user = qs.getResource("user");
    users.add(user.getLocalName());
}
String results = "";
for(String user : users) {
    results+=user+" ";
}
return results;

I get a NullPointerException at the line where I initialize the QueryExecution variable.

Here is the stack trace of the error :

java.lang.NullPointerException
        at org.apache.jena.query.ARQ.isTrue(ARQ.java:650)
        at org.apache.jena.sparql.lang.ParserBase.<init>(ParserBase.java:292)
        at org.apache.jena.sparql.lang.SPARQLParserBase.<init>(SPARQLParserBase.java:43)
        at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11Base.<init>(SPARQLParser11Base.java:22)
        at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.<init>(SPARQLParser11.java:4974)
        at org.apache.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:91)
        at org.apache.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:52)
        at org.apache.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:34)
        at org.apache.jena.query.QueryFactory.parse(QueryFactory.java:147)
        at org.apache.jena.query.QueryFactory.create(QueryFactory.java:79)
        at org.apache.jena.query.QueryFactory.create(QueryFactory.java:52)
        at org.apache.jena.query.QueryFactory.create(QueryFactory.java:40)
        at fr.conceptit.tuto.web.Main.work(Main.java:71)
        ...

I'm deploying my application via Karaf maybe the problem comes from there. Here's a screenshot of my bundles list on Karaf. Notice that I added all Jena Jars.Karaf Bundles list


Solution

  • Thanks to @AndyS comment I was able to figure out a way to make it work. He pointed in the right direction saying :

    NPE on ARQ.isTrue : ARQ isn't initialized properly

    Indeed that was the problem and I resolved it by initializing the ARQ and not JenaSystem as he suggested. Basically I added org.apache.jena.query.ARQ.init() before there has been any call to Jena and it worked. Thanks again for your help and support.