java.lang.NullPointerException 在 org.omnifaces.util.selectitems.SelectItemsCollector.collectFromParent

java.lang.NullPointerException at org.omnifaces.util.selectitems.SelectItemsCollector.collectFromParent

我正在使用以下内容实现一个 JSF 项目:

我想在我项目的所有自定义实体上实施 omnifaces.SelectItemsConverter,我已经按照 http://showcase.omnifaces.org/converters/SelectItemsConverter 中的指南进行操作。

问题是,当我 select 我的 selectOneMenu 上的一个选项时,转换器被调用了 3 次,第一次调用的值是 selected 的其余部分次与 null 所以最后一个 NullPointerException 并抛出堆栈跟踪是

java.lang.NullPointerException
    at org.omnifaces.util.selectitems.SelectItemsCollector.collectFromParent(SelectItemsCollector.java:109)
    at org.omnifaces.util.selectitems.SelectItemsUtils.findValueByStringConversion(SelectItemsUtils.java:50)
    at org.omnifaces.converter.SelectItemsConverter.getAsObject(SelectItemsConverter.java:95)
    at org.apache.myfaces.shared.renderkit.RendererUtils.getConvertedUISelectOneValue(RendererUtils.java:989)
    at org.apache.myfaces.shared.renderkit.html.HtmlMenuRendererBase.getConvertedValue(HtmlMenuRendererBase.java:153)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:662)
    at javax.faces.component.UIInput.validate(UIInput.java:598)
    at javax.faces.component.UIInput.processValidators(UIInput.java:274)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:788)
    at org.apache.myfaces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:213)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1019)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at org.primefaces.component.api.UITabPanel.visitTree(UITabPanel.java:920)
    at javax.faces.component.UIForm.visitTree(UIForm.java:345)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1047)
    at javax.faces.component.UIComponentBase.visitTree(UIComponentBase.java:1191)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartialExecute(PartialViewContextImpl.java:430)
    at org.apache.myfaces.context.servlet.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:411)
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:60)
    at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:85)
    at org.omnifaces.context.OmniPartialViewContext.processPartial(OmniPartialViewContext.java:136)
    at javax.faces.component.UIViewRoot$ProcessValidatorPhaseProcessor.process(UIViewRoot.java:1797)
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1655)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:909)
    at org.apache.myfaces.lifecycle.ProcessValidationsExecutor.execute(ProcessValidationsExecutor.java:38)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:196)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:143)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:105)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access[=12=]0(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我的问题是我找不到失败原因,因为异常是在我的代码之外抛出的,我代码的相关部分是:

<h:selectOneMenu id="entity"
        value="#{backingBean.parentEntity.entity}"
        required="#{not empty param[save.clientId] or not empty param[anotherAction.clientId]}"
        converter="omnifaces.SelectItemsConverter"
        label="Entity">
    <p:ajax listener="#{backingBean.onEntityChanged}"
        onstart="$( '#parentForm\:accordionPanel\:data' ).addClass('loadinggif');"
        oncomplete="$( '#parentForm\:accordionPanel\:data' ).removeClass('loadinggif');"
        update="segment :parentForm:growl :solicitud:guardar :parentForm:accordionPanel:consultantInformation :parentForm:accordionPanel:data"
        process="entity segment :parentForm:accordionPanel:consultantInformation :parentForm:accordionPanel:data"
        global="false" />
    <f:selectItem itemLabel="Select ..." noSelectionOption="true"/>
    <f:selectItems value="#{backingBean.entities}" var="entity" itemLabel="#{entity.nombre}" itemValue="#{entity}" />
</h:selectOneMenu>

在我的 Backing bean 和自定义对象中:

public class BackingBean implements Serializable {
    private ParentObject parentEntity;
    private List<Entity> entities;

    @PostConstruct
    public void init() {
        this.parentEntity = new ParentObject();
        user = (UserDTO) SecurityContextHolder.getContext()
                    .getAuthentication().getPrincipal();
    }

    /**
     * This method get called by a 
     * <f:metadata>
     *  <f:viewParam name="id" value="#{backingBean.parentObject.id}" />
     *  <f:viewAction action="#{backingBean.loadObject}" />
     * </f:metadata>
     */
    public void loadObject() {
        inicializarListas();
    }

    public void initializeLists() {
        entities = user.getEntities();
            if (entities == null || entities.isEmpty()) {
                entities = someController.findEntities();
            }
    }

    //getter and setter for parentEntity  
}

任何想法

谢谢。

编辑

我昨天正在处理这个问题并注意到了拼写错误,但即使修复了它,转换器在第一次调用时也获得了正确的值,其余时间为空值。

这个问题是three-fold。

  1. 抛出来自 SelectItemsCollector.java:109 的令人困惑的 NPE,而应该抛出异常:

    106     } else {
    107
    108         // A value binding was specified, but of a type we don't support.
    109         throw new IllegalArgumentException(String.format(ERROR_UNKNOWN_SELECT_TYPE, value.getClass().toString()));
    110     }
    

    因此,value 为 null,而代码并未预料到这一点。这确实是 OmniFaces 中的一个错误。我只是 fixed 2.3.


  2. 在 Mojarra 中,当 valuenull 时,此代码首先不会到达,但在 Myfaces 中与您的情况一样(在 RichFaces 中,如 OmniFaces 中所报告的) issue 185) 居然是。就是这样。升级到 OmniFaces 2.3(目前仅以 SNAPSHOT 形式提供)后,世界应该又好了。


  3. select 项 valuenull,因为您在 value.[=21= 中打错了托管 bean 名称]

    <f:selectItems value="#{bakingBean.entities}" ... />
    

    是豆子,不是蛋糕。