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
Run in debug mode and place a breakpoint as it enters the gamevariables method. Check every object has been initialised before being used. If this is a regularly called method then it might be better to first run it and allow it to crash, thus letting you identify the line where it throws the exception.
How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.
NullPointerException is thrown when an application attempts to use an object reference that has the null value. These include: Calling an instance method on the object referred by a null reference. Accessing or modifying an instance field of the object referred by a null reference.
NullPointerException is raised in an application when we are trying to do some operation on null where an object is required. Some of the common reasons for NullPointerException in java programs are: Invoking a method on an object instance but at runtime the object is null.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With