Null pointer exception on a radio button

Asked

Viewed 203 times

2

I’m trying to put two Primefaces radio buttons on one page xhtml:

<p:selectOneRadio id="fis_jur" layout="custom" value="#{usuario.fis_jur}" required="true">
    <f:selectItem itemLabel="Física" itemValue="1"/>
    <f:selectItem itemLabel="Jurídica" itemValue="2"/>
    <p:ajax event="keyup" update="pessoa"/>
</p:selectOneRadio>
<p:panelGrid columns="5">
    <p:outputLabel for="fis_jur" value="Pessoa"/>
    <p:radioButton id="fisico" for="fis_jur" itemIndex="0"/>
    <p:outputLabel value="Física"/>
    <p:radioButton id="juridico" for="fis_jur" itemIndex="1"/>
    <p:outputLabel value="Jurídica"/>
</p:panelGrid>
<p:outputLabel id="pessoa" value="#{usuario.pessoa()}"/>
@ManagedBean
@RequestScoped
public class Usuario {
    public Usuario() {
        fis_jur = 1;
    }
    private int fis_jur;
    //getter-setter
    public String pessoa() {
        return fis_jur == 1 ? "Física" : "Jurídica";
    }
}

The problem is when I try to access the page, a Nullpointerexception is launched and does not specify which line (from the page), but when I comment this block of the page xhtml, she doesn’t accuse mistakes, so I’m afraid the problem is in this part, but I can’t see where.

Stacktrace

java.lang.NullPointerException
    at org.primefaces.component.radiobutton.RadioButtonRenderer.encodeMarkup(RadioButtonRenderer.java:51)
    at org.primefaces.component.radiobutton.RadioButtonRenderer.encodeEnd(RadioButtonRenderer.java:41)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:89)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:60)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeRow(PanelGridRenderer.java:142)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeStaticBody(PanelGridRenderer.java:108)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeBody(PanelGridRenderer.java:63)
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:81)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
  • Have you tried debugging? placed the Getters and Setters of fis_jur?

  • How could I debug it? I just Gero the War and upload to the Tomcat. Yes, the getters-setters are there.

  • I don’t understand why you have the p:radioButton and the p:outputLabel with the same attribute for pointing to the p:selectOneRadio. For me the p:outputLabel should have the attribute for pointing to the id of p:radioButton, and remove the attribute for of p:radioButton. More info: http://www.primefaces.org/showcase/ui/input/oneRadio.xhtml

  • @Wakim, I did so because of the attribute "required" that puts an asterisk on the label who has the for aimed at her.

  • But you can’t add the required="true" also in the radioButton? You putting the for of outputPanel to the fis_jur, linked the two, the p:outputPanel is not related to the radioButton.

  • @Wakim, is more or less like this: Pessoa ? ( ) Physical ( ) Legal.

  • @Wakim, yes, it is possible but I believe that this would not be very viable, because it is a label for two radios.

  • Ah, I understand what you want to do... To know the cause of this NullPointer just looking at the same source code.

  • @Wakim, the source code is there.

  • When I said source code, it was the Primefaces hehe. Whenever these problems occur it is good to turn to the source code of the library.

  • I reveal that this exception did not happen, but I do not remember what I changed and I do not have a version. Since it’s practically the same as the Primefaces showcase, I can’t understand the reason for this mistake.

  • @Patrick believes that your user uses the mouse to select these items, so you do not need to put event="keyup" which is for keystrokes.

  • @Luídne I’m almost sure that the keyup considers mouse clicks as well.

Show 8 more comments

1 answer

1

Normally I use the Radiobutton variable as String, you are using int. Another thing, boot to @Postconstruct

String fis_jur;
@PostConstruct
public void init() {
    fis_jur = "teste";
}

See also examples on primefaces

Browser other questions tagged

You are not signed in. Login or sign up in order to post.