BRabbit27
BRabbit27

Reputation: 6623

DataTable not displaying info JSF

I'm having trouble with a DataTable. I have a SelectOneMenu component with 3 options. Every option should render a DataTable with different information.

My xhtml page is working (I tested it with a simple code), but it seems that my backing bean is not working as I was expecting. When I run the page throws an error message.

My xhtml looks like

<h:form>
        <h:outputLabel for="reporte" value="Tipo de reporte: " />
        <h:selectOneMenu id="reporte"
                         value="#{reportController.type}">
            <f:selectItem itemLabel="Seleccione tipo de reporte..." itemValue="null" />
            <f:selectItems value="#{reportController.reportType}" />
            <f:ajax execute="@form" render="@form" />
        </h:selectOneMenu>




        <h:panelGrid id="porSistema"
                     rendered="#{reportController.type == 'Por sistema'}">
            <h:dataTable value="#{reportController.list}" var="item"
                         border="1">
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Nombre del archivo"/>
                    </f:facet>
                    <h:outputText value="#{item.fileName}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Registros correctos"/>
                    </f:facet>
                    <h:outputText value="#{item.registersOk}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Registros incorrectos"/>
                    </f:facet>
                    <h:outputText value="#{item.registersNok}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Registros procesados"/>
                    </f:facet>
                    <h:outputText value="#{item.registers}"/>
                </h:column>
            </h:dataTable>
        </h:panelGrid>
    </h:form>

The bean is

@ManagedBean
@ViewScoped
public class ReportController {

    List<Object> list = new ArrayList<Object>();
    String type;
    String[] reportType = new String[]{"Por sistema",
        "Por programa y componente",
        "Por estatus de pago"};

    @PostConstruct
    public void init() {
        if (type.equals("Por sistema")) {
            list.add(new ReporteSistema("A", "B", "C", "D"));
        } else if (type.equals("Por programa y componente")) {
            list.add(new ReporteProgramaComponenteA("E", "F", "G", "H", "I", "J"));
        } else if (type.equals("Por estatus de pago")) {
            list.add(new ReporteProgramaComponenteB("K", "L", "M", "N"));
        }
    }

    public String[] getReportType() {
        return reportType;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }
}

and the stack trace is

com.sun.faces.mgbean.ManagedBeanCreationException: Se ha producido un error al realizar la inyección de recurso en el bean administrado reportController
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:229)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.getCurrentSelectedValues(MenuRenderer.java:648)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:749)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at mx.gob.sagarpa.NoCacheFilter.doFilter(NoCacheFilter.java:29)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    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:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: com.sun.faces.spi.InjectionProviderException
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:119)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:99)
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:223)
    ... 56 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor85.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:117)
    ... 58 more
Caused by: java.lang.NullPointerException
    at mx.gob.sagarpa.beans.ReportController.init(ReportController.java:32)
    ... 62 more

Upvotes: 1

Views: 5853

Answers (2)

BalusC
BalusC

Reputation: 1108632

You seem to be misunderstanding the purpose of the @PostConstruct. This annotation is to be used to specify methods which are to be executed directly after bean's construction. This annotation allows you to execute code after injection of managed properties specified by @ManagedProperty and all other dependencies such as @Inject or @EJB. Those injected properties and dependencies are namely not available inside the bean's constructor (a fully constructed instance of the bean has to be obtained first before all injections can take place, of course). The @PostConstruct of a view scoped bean is not invoked when you send an ajax request on it. It's only invoked when you initially open the page in the browser.

You actually want to invoke that method when the ajax event is fired. You need to change the view and bean as follows in order to get it to work.

<h:selectOneMenu id="reporte" value="#{reportController.type}">
    <f:selectItem itemLabel="Seleccione tipo de reporte..." itemValue="null" />
    <f:selectItems value="#{reportController.reportType}" />
    <f:ajax execute="@form" listener="#{reportController.changeType}" render="@form" />
</h:selectOneMenu>

(note the new listener attribute)

and

public void changeType() { // No, do not put @PostConstruct on it!
    if ("Por sistema".equals(type)) {
        list.add(new ReporteSistema("A", "B", "C", "D"));
    } else if ("Por programa y componente".equals(type)) {
        list.add(new ReporteProgramaComponenteA("E", "F", "G", "H", "I", "J"));
    } else if ("Por estatus de pago".equals(type)) {
        list.add(new ReporteProgramaComponenteB("K", "L", "M", "N"));
    }
}

Also note that I swapped the both sides of the equals() so that you can guarantee that it will never throw NPE. If the type happen to be null, then it will simply be compared as non-equal. When you actually invoked equals() on something which is null, you will guaranteed get a NPE (that's what the whole exception stands for! see also its javadoc).

Upvotes: 1

Mr.J4mes
Mr.J4mes

Reputation: 9266

Your problem is obviously stated in the stacktrace. Your init method threw a NullPointerException. To be precise, your type variable was not given any value. You cannot call equals function with it.

Upvotes: 1

Related Questions