【问题标题】:NPE Nullpointer exception in spring webflowSpring Webflow中的NPE Nullpointer异常
【发布时间】:2019-06-10 16:57:18
【问题描述】:

我在 spring webflow 中遇到奇怪的错误。

2019-06-07 15:04:39.026 ERROR 29470 --- [nio-8096-exec-8] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at org.springframework.webflow.execution.repository.support.AbstractFlowExecutionRepository.getConversation(AbstractFlowExecutionRepository.java:170) ~[spring-webflow-2.4.4.RELEASE.jar:2.4.4.RELEASE]
    at org.springframework.webflow.execution.repository.support.AbstractFlowExecutionRepository.getLock(AbstractFlowExecutionRepository.java:125) ~[spring-webflow-2.4.4.RELEASE.jar:2.4.4.RELEASE]
    at org.springframework.webflow.executor.FlowExecutorImpl.launchExecution(FlowExecutorImpl.java:142) ~[spring-webflow-2.4.4.RELEASE.jar:2.4.4.RELEASE]
    at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:263) ~[spring-webflow-2.4.4.RELEASE.jar:2.4.4.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23]

这发生在 spring webflow 库中,我完全不知道为什么。 我的流程看起来像:

<action-state id="checkProductAvailable">
       <evaluate expression="knowledgeSourceService.test(requestParameters.productUUid, requestParameters._eventId)"/>
           <transition on="internet" to="validateAndProceedToSummary">
               <evaluate expression="knowledgeSourceService.extractCustomerSelectedSourceFromRequest(requestParameters)"
                         result="flowScope.customerSelectedSource"/>
               <evaluate expression="knowledgeSourceService.enhanceProductData(requestParameters.productUUid)"
                         result="conversationScope.selectedProduct"/>
           </transition>
           <transition on="requireProducts" to="products" />
   </action-state>


   <decision-state id="validateAndProceedToSummary">
    <if test="knowledgeSourceService.updateCustomerSelection(flowScope.customerSelectedSource, flowScope.productList.selectedProduct.customerSelectedSource)"
        then="proceedToSummary" else="invalidateFormState"/>
</decision-state>

当我将转换从 validateAndProceedToSummary 更改回 products 时,它可以正常工作而不会出现任何错误。但是对于这个validateAndProceedToSummary 决策状态,它会失败。

products是视图状态:

<view-state id="products" view="invest-flow-products" model="productList">

你能帮帮我吗?

knowledgeSourceService.updateCustomerSelection(flowScope.customerSelectedSource, flowScope.productList.selectedProduct.customerSelectedSource)

正在调用方法:

public boolean updateCustomerSelection(String old, String new) {

但据我所知,成功路径 (proceedToSummary) 并没有达到此方法(我设置断点)。只有在执行失败路径时才会到达 (invalidateFormState)。所以像一些先决条件没有被满足。但不知道是哪个。我们可以调试它吗?

【问题讨论】:

  • updateCustomerSelection() 返回的是什么?

标签: spring spring-webflow-2


【解决方案1】:

如果 updateCustomerSelection() 返回 wrapper boolean,那么我们需要检查 null 值,因为 wrapper boolean 也有可能保存 null 值。

下面的代码应该可以解决 NPE 问题:

if (null != updateCustomerSelection() && updateCustomerSelection()) {
    //code
}

或者如果你使用的是apache commons library,那么你可以写如下:

if (BooleanUtils.isTrue(updateCustomerSelection())) {
     //code
}

【讨论】:

  • 它返回简单的布尔值,而不是 Boolean.java 类型。并且它没有达到这个方法的执行。误会请见谅。检查答案,我已经更新了。
猜你喜欢
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2017-05-11
  • 2014-01-31
  • 2011-09-11
  • 1970-01-01
  • 2012-02-17
相关资源
最近更新 更多