我开始使用Jena(使用com.hp.hpl.jena
),我遇到了一个奇怪的问题。
我正在使用示例本体,其中Expert
有一个Name
作为字符串文字和Research
es。我想使用Expert
列出OntClass.listDeclaredProperties()
的属性,但会产生NullPointerException
。以下是代码示例:
OntClass expert = model.getOntClass(ONTOLOGY_URL + "Expert");
Iterator it = expert.listInstances();
// print out the instances of the Expert class
while (it.hasNext()) {
Individual oi = (Individual) it.next();
System.out.println(oi.getLocalName());
//get the properties of the instances of the Expert class
Iterator ipp = expert.listDeclaredProperties(false); // NPE here
while (ipp.hasNext()) {
OntProperty p = (OntProperty) ipp.next();
//print out property name and its values
System.out.println(p.getLocalName());
for (Iterator ivv = oi.listPropertyValues(p); ivv.hasNext();) {
String valuename = ivv.next().toString();
System.out.println(valuename);
}
}
}
这是完整的堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at com.hp.hpl.jena.ontology.impl.OntClassImpl.testDomain(OntClassImpl.java:940)
at com.hp.hpl.jena.ontology.impl.OntClassImpl.hasDeclaredProperty(OntClassImpl.java:628)
at com.richou.jenasandbox.client.Main.getProperty(Main.java:68)
at com.richou.jenasandbox.client.Main.main(Main.java:50)
任何可能出错的想法?