简体   繁体   中英

How to validate an input text component based on the value of a radio button component

I am trying to validate an input text component based on the value of a radio button component, however it throws a NullPointerException and I don't understand why.

Here's the view:

<h:form id="formId">
    <p:inputText validator="#{searchBean.validate}" id="name"
        required="true" requiredMessage="should contain only characters">
        <f:validateRegex pattern="[a-zA-Z]*" />
        <f:validator validatorId="inputValidator" />
    </p:inputText>
    <p:selectOneRadio value="#{searchBean.searchForm.selectedRadio}" id="customRadio">
        <f:selectItem itemLabel="city" itemValue="1" />
        <f:selectItem itemLabel="zipcode" itemValue="2" />
        <f:selectItem itemLabel="state" itemValue="3" />
    </p:selectOneRadio>
    <p:commandButton value="Search" action="results.jsf" />
</h:form>

Here's the validator:

public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    UIViewRoot ui = new UIViewRoot();
    UIInput radio = (UIInput) ui.findComponent("formId:customRadio");
    boolean convertedAndValidatedValue = (Boolean) radio.getValue(); // This line is throwing the exception.
    System.out.println("Validate"+convertedAndValidatedValue);

    if (searchForm.selectedRadio.equals("city")) {
        validateForCity(convertedAndValidatedValue);
    } else if (searchForm.selectedRadio.equals("zipcode")) {
        validateForZipcode(convertedAndValidatedValue);
    } else {
        validateForState(convertedAndValidatedValue);
    }
}

Here's the exception:

javax.faces.FacesException
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:80)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    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:679) 
Caused by: java.lang.NullPointerException
    at com.placessearch.action.SearchBean.validate(SearchBean.java:76)
    at javax.faces.component.UIInput.validateValue(UIInput.java:1142)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1204)
    at javax.faces.component.UIInput.processValidators(UIInput.java:693)
    at javax.faces.component.UIForm.processValidators(UIForm.java:240)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1159)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72) 
    ... 19 more

JSF components are converted and validated in the order as they appear in the component tree. Your validator is attached to an input text component which appears before the radio button component. However, you're trying to access the converted and validated value of the radio button component, while it is not been converted and validated at that point yet.

boolean convertedAndValidatedValue = (Boolean) radio.getValue();

It would only return null which you're in turn trying to autobox to boolean which would only cause NullPointerException .

You need to get the submitted value instead. Please note that the item values which you're specifying there are absolutely not booleans. It are numbers. Also note that the submitted value is always String (simply because that's exactly the type which request.getParameter() returns and also because at this moment it's not been converted by JSF/EL yet).

String submittedRadioValue = (String) radio.getSubmittedValue();

if (submittedRadioValue != null) {
    int convertedRadioValue = Integer.valueOf(submittedRadioValue);
    // ...
}

Update : another cause which I overlooked due to its unexpected nature is the following:

UIViewRoot ui = new UIViewRoot();

This is definitely also not right. You're supposed to retrieve the current one from the faces context instead of manually creating a completely empty one.

UIViewRoot ui = context.getViewRoot();

Otherwise radio would still be null and thus be another potential cause of the NullPointerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM