Jsf 2 如何验证selectOneMenu';s值多少?

Jsf 2 如何验证selectOneMenu';s值多少?,jsf-2,Jsf 2,我正在验证我的表单,我可以获取textbox的值,但总是在中检索空值 uiClass变量(错误:java.lang.NullPointerException): 有什么建议吗 例外情况: WARNING: /jsf/report/Edit.xhtml @39,99 listener="#{studentController.validate}": java.lang.NullPointerException javax.el.ELException: /jsf/report/Edit.xhtml

我正在验证我的表单,我可以获取textbox的值,但总是在中检索空值 uiClass变量(
错误:java.lang.NullPointerException
): 有什么建议吗

例外情况:

WARNING: /jsf/report/Edit.xhtml @39,99 listener="#{studentController.validate}": java.lang.NullPointerException
javax.el.ELException: /jsf/report/Edit.xhtml @39,99 listener="#{studentController.validate}": java.lang.NullPointerException
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
    at com.sun.faces.facelets.tag.jsf.core.DeclarativeSystemEventListener.processEvent(EventHandler.java:131)
    at javax.faces.component.UIComponent$ComponentSystemEventListenerAdapter.processEvent(UIComponent.java:2464)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
    at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2168)
    at com.sun.faces.application.ApplicationImpl.invokeComponentListenersFor(ApplicationImpl.java:2116)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:288)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:246)

Caused by: java.lang.NullPointerException
    at entities.StudentController.validate(StudentController.java:163)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)  

@ManagedBean()
@SessionScoped
public class StudentController implements Serializable {
    public void validate(ComponentSystemEvent event) {
        FacesContext fc = FacesContext.getCurrentInstance();
        UIComponent components = event.getComponent();

        UIInput uiName = (UIInput) components.findComponent("frmStudent:name");
        UIInput uiClass = (UIInput) components.findComponent("frmStudent:myclass");

        String name = uiName.getLocalValue().toString();
        String myclass = uiClass.getLocalValue().toString();

        if (name.equals("") || name == null || name.isEmpty()) {
            FacesMessage msgName = new FacesMessage("Please enter name !");
            fc.addMessage(components.getClientId(), msgName);
            fc.renderResponse();
        } else if (myclass.equals("") || myclass == null || myclass.isEmpty()) {
            FacesMessage msgClass = new FacesMessage("Please select a class !");
            fc.addMessage(components.getClientId(), msgClass);
            fc.renderResponse();
        }
    }
}
我的XHTML

<h:message for="textPanel" style="color:red;" />
<h:panelGrid id="textPanel" columns="4">
  <f:event listener="#{studentController.validate}" type="postValidate" />
    <h:inputText id="name" value="#{studentController.selected.name}" size="20" />
    <h:selectOneMenu id="myclass" value="#{studentController.selected.myclass}" style="width:180px;">
        <f:selectItem itemLabel="Select ..." itemValue="#{null}" noSelectionOption="true" />
        <f:selectItems value="#{studentController.classItems}"/>
    </h:selectOneMenu>
</h:panelGrid>

为什么不使用必需的属性

 <h:inputText id="name" required="true" requiredMessage="Please enter name !" value="#{studentController.selected.name}" size="20" />
    <h:selectOneMenu id="myclass" required="true" requiredMessage="Please select a class !" value="#{studentController.selected.myclass}" style="width:180px;">
        <f:selectItem itemLabel="Select ..." itemValue="#{null}" noSelectionOption="true" />
        <f:selectItems value="#{studentController.classItems}"/>
    </h:selectOneMenu>


请发布您的异常stacktrace,此外,您应该更改
name.equals(“”| | name==null
name==null | | name.equals(“”
),因为如果name是
null,它将导致NPE,所以除了selectOneMenu的值外,它是可以的。是的,我现在就发