我正在尝试根据单选按钮组件的值验证输入文本组件,但它会抛出NullPointerException
,我不明白为什么。
以下是观点:
<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>
这是验证器:
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);
}
}
以下是例外:
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
答案 0 :(得分:4)
JSF组件按其在组件树中显示的顺序进行转换和验证。您的验证器附加到输入文本组件,该组件在单选按钮组件之前出现。但是,您尝试访问单选按钮组件的已转换和验证值,而尚未在该点进行转换和验证。
boolean convertedAndValidatedValue = (Boolean) radio.getValue();
它只会返回null
,而您又会尝试自动装箱到boolean
,这只会导致NullPointerException
。
您需要获取提交的值。请注意,您指定的项目值绝对不是布尔值。这是数字。另请注意,提交的值总是 String
(因为这正是request.getParameter()
返回的类型,也因为此时它尚未被JSF / EL转换)
String submittedRadioValue = (String) radio.getSubmittedValue();
if (submittedRadioValue != null) {
int convertedRadioValue = Integer.valueOf(submittedRadioValue);
// ...
}
更新:由于其出乎意料的特性,我忽略了另一个原因如下:
UIViewRoot ui = new UIViewRoot();
这绝对也不对。您应该从faces上下文中检索当前的一个,而不是手动创建一个完全空的。
UIViewRoot ui = context.getViewRoot();
否则radio
仍然是null
,因此是NullPointerException
的另一个潜在原因。