Search code examples
javanetbeanssparqlsemantic-web

Sparql query, fetching output


I am trying to fetch resultset of a query made on the endpoint linkedlifedata.com/sparql and the object which i want to fetch is "indication" which is a paragraph. How do i print it in netbeans? The code is:-

String qs1 = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"+
"PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>"+
"select ?indication"+
"where {"+
"?drug drugbank:indication ?indication."+
"?drug drugbank:genericName ?drugname."+
"filter(regex(?drugname, 'Omalizumab','i'))"+
"}LIMIT 100 ";

com.hp.hpl.jena.query.Query query = QueryFactory.create(qs1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://linkedlifedata.com/sparql", query);
String o;
try{
ResultSet results = qexec.execSelect(); 
for ( ; results.hasNext() ; )
{
QuerySolution soln = results.nextSolution() ;
o = soln.get("indication").toString();
System.out.println(o);
}
} 
finally {
qexec.close();
}
}

I applied the above method. But it's displaying nullpointer exception.The query is running fine(checked on the endpoint). I have previously obtained drugname(a small string) using isLiteral() function, but for "indication" which is a paragraph, it's not happening. plz HELP, IT'S URGENT.Thanx


Solution

  • I didn't spot this at all until I ran it. Your problem is this:

    "select ?indication"+
    "where {"+
    

    No white space at the end of the select line, so the query is actually:

    ... select ?indicationwhere { ... }
    

    hence no bindings for ?indication, and a null pointer exception. Stick a newline or space in there.