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

1

5

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

Moses answered 27/2, 2019 at 12:10 Comment(6)
So it happens inside QueryExecutionFactory.create? Catch the exception and print a stack traceTranslator
jetbrains.com/help/idea/using-breakpoints.htmlCurtsy
@JoakimDanielson indeed it happens inside QueryExecutionFactory.create . I added the stack trace for more informationMoses
NPE on ARQ.isTrue : ARQ isn't initialized properly;I can't see why not though. Which version are you running? (1) Have you repackaged the code at all? (2) If no, try adding JenaSystem.init() before there has been any call to Jena.Pennant
@Pennant I don't unsderstand what you mean by repackaged the code I use mvn clean install, did you mean I have to use mvn package? I added JenaSystem.init() didn't do any better still have the same the NPE. Also maybe I wasn't clear enough but I'm using Karaf. I gave all the jena jars I'm adding a screenshot of my bundles maybe the problem comes from there.Moses
Initialization is base on Java ServiceLoader mechanism. If Jena is packaged (e.g. shade plugin), then that has to combine the control files: jena.apache.org/documentation/notes/jena-repack.html. Classloaders also come into play if it is OSGi.Pennant
M
8

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.

Moses answered 28/2, 2019 at 9:53 Comment(4)
JenaEngine is not part of Jena.Pennant
JenaSystem.init() is the right call rather than ARQ.init but both should work.Pennant
@Pennant you're actually right it was a typo I meant JenaSystem and not JenaEngine I edited my answer. Also I tried JenaSystem.init() didn't work for me. Maybe it comes from the fact that I'm using Jena with OSGI. Thanks again for your intel it helped me resolve my problem.Moses
thanks. org.apache.jena.query.ARQ.init() also worked for mePiggott

© 2022 - 2024 — McMap. All rights reserved.