2012-11-25 62 views
1

我想获取在端点linkedlifedata.com/sparql上做出的查询的结果集和我想要获取的对象是“迹象”,这是一个段落。我如何在netbeans中打印它?代码是: -Sparql查询,获取输出

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(); 
} 
} 

我应用了上面的方法。但它显示空指针异常。查询运行良好(在端点上检查)。我以前使用isLiteral()函数获得了药物名称(一个小字符串),但对于“指示”这是一个段落,它没有发生。 plz帮助,它是紧急的。谢谢

+0

你可以告诉我们空指针在哪里出现(哪一行完全) – Jerven

+0

在o = soln.get(“indication”)。toString(); – user1851265

回答

2

我没有发现这件事,直到我跑了它。你的问题是这样的:

"select ?indication"+ 
"where {"+ 

在选择行的末尾没有空格,所以查询居然是:

... select ?indicationwhere { ... } 

因此没有绑定?indication,和一个空指针异常。在那里粘贴换行符或空格。