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
QueryExecutionFactory.create
? Catch the exception and print a stack trace – TranslatorQueryExecutionFactory.create
. I added the stack trace for more information – MosesARQ.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 addingJenaSystem.init()
before there has been any call to Jena. – Pennantmvn clean install
, did you mean I have to usemvn package
? I addedJenaSystem.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