Gerson Sosa
Gerson Sosa

Reputation: 366

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

I'm implementing a JSF project with the following:

I want to implement omnifaces.SelectItemsConverter on all the custom entities of my project, I already followed the guide in http://showcase.omnifaces.org/converters/SelectItemsConverter.

The problem is that when I select an option on my selectOneMenu the converter gets called 3 times the first time is called with the value selected the rest of the times with null so at the end a NullPointerException and is thrown the stack trace is

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$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.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)

My problem is I can't find the failure since the exception is thrown outside my code, the relevant part of my code is:

<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>

And in my Backing bean and custom objects:

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  
}

Any ideas

Thanks.

EDIT

I was working yesterday on the problem and noticed the typo but even fixing it the converter is getting the correct value the first time is called and null values the rest of the times.

Upvotes: 2

Views: 1925

Answers (1)

BalusC
BalusC

Reputation: 1109735

This problem is three-fold.

  1. That confusing NPE from SelectItemsCollector.java:109 is thrown while an exception should be thrown:

    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     }
    

    So, value is null where code didn't expect that. This is indeed a bug in OmniFaces. I just fixed it for 2.3.


  2. In Mojarra, this code is in first place never reached when value is null, but in Myfaces as in your case (and in RichFaces, as reported in OmniFaces issue 185) it unexpectedly is. It is what it is. Once you upgrade to OmniFaces 2.3 (currently only available as SNAPSHOT), world should be well again.


  3. The select items value is null, because you typo'ed the managed bean name in the value.

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

    It's a bean, not a cake.

Upvotes: 3

Related Questions