简体   繁体   English

如何在Java中使用Jena API处理查询的空结果

[英]How to Handle null result of a Query using Jena API in Java

I am using Jena API. 我正在使用Jena API。 Every thing works fine with me, except when query match (ie result of Query is null) is not found in an Ontology Model. 除了在本体模型中找不到查询匹配(即查询结果为空)时,所有事情对我来说都很好。 Then nothing is displayed in Browser using servlet, and null pointer exception is displayed at console. 然后,在使用Servlet的浏览器中不显示任何内容,并且在控制台上显示空指针异常。 ie given below. 即在下面给出。 Can anyone guide me how to handle this null pointer exception? 谁能指导我如何处理此空指针异常? thank you. 谢谢。

Jun 04, 2014 3:37:22 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [main.DevInsta] in context with path [/FMOnt] threw exception
java.lang.NullPointerException
    at main.DevInsta.doGet(DevInsta.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Initial Solution 初始解决方案

At the time of this answer, the query string used in this question is not posted, nor is the code which is used around where the NullPointerException occurs. 在回答此问题时,未发布此问题中使用的查询字符串,也没有发布NullPointerException发生位置附近使用的代码。 This is a first-pass at assisting with the answer. 这是协助解答的第一步。

My usual source of a NullPointerException when working with a QueryExecution comes from one of two places: 在使用QueryExecution时,我通常会产生NullPointerException来源来自以下两个地方之一:

Aggregates 骨料

If your query is using GROUP BY x , but your data doesn't bind anything to x , you can end up with a ResultSet that looks like the following: 如果您的查询使用的是GROUP BY x ,但您的数据未将任何内容绑定到x ,那么您可以得到一个类似于以下内容的ResultSet

---------------
|  ?x  |  ?y  |
---------------
|      |      |
---------------

You can get an unexpected true result from hasNext() , and then a subsequent NullPointerException when attempting to retrieve elements. 您可能会从hasNext()获得意外的true结果,然后在尝试检索元素时收到随后的NullPointerException The solution is, for aggregate queries, to test if the first result's expected bindings contain any null values as well. 对于聚合查询,解决方案是测试第一个结果的预期绑定是否也包含任何null值。

Optional 可选的

If your query includes an optional block, then it may not get bound for a particular solution, and you need to test for it: 如果您的查询包含一个可选块,则它可能不受特定解决方案的约束,因此您需要对其进行测试:

SELECT ?x ?y {
   ?x rdf:type urn:ExampleClass .
   OPTIONAL { ?x urn:p ?y }
}

When you iterate over QuerySolution objects, the value returned by getting a variable may be null , and you need to account for it in your code. 当您遍历QuerySolution对象时,通过获取变量返回的值可能为null ,并且您需要在代码中进行说明。

WebService Help WebService帮助

One thing which I would suggest, if your ResultSet isn't very large, is to use a ResultSetRewindable (provided by ResultSetFactory.copyResults ). 如果您的ResultSet不是很大,我建议的一件事是使用ResultSetRewindable (由ResultSetFactory.copyResults提供)。 In your web application, if you use a decent logging library, you can test things like logger.isDebugEnabled() . 在Web应用程序中,如果使用体面的日志记录库,则可以测试logger.isDebugEnabled()类的东西。 If it is, you can print a diagnostic value for the query using ResultSetFormatter . 如果是这样,则可以使用ResultSetFormatter为查询打印诊断值。 This can be extremely useful when attempting to understand why your rendering of a query is giving you unexpected results: 当试图理解为什么呈现查询给您带来意外结果时,这可能非常有用:

final QueryExecution exec = ...;
final ResultSetRewindable r = ResultSetFactory.copyResults(exec.execSelect());
exec.close();
// Assuming slf4j
if( logger.isDebugEnabled() ) {
    final ByteArrayOutputStream results = new ByteArrayOutputStream();
    ResultSetFactory.out(results,r);
    r.reset();
    logger.debug("Rendering query solution:\n{}", results);
}
// Continue on like you normally would 

If you don't want to do this in general (maybe doGet is called far too often and/or your results are too large and/or this is a complex system where the error happens rarely), then you can always put a try{..}catch(..){..} -block around where the exception is occurring, and only print a copy of the results when this particular exception is triggered. 如果您一般不希望这样做(也许doGet被调用得太频繁和/或您的结果太大,并且/或者这是一个错误很少发生的复杂系统),那么您可以随时try{..}catch(..){..} -阻止发生异常的位置,并且仅在触发此特定异常时才打印结果的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM