-2
    <pre><h:form>
        <h:panelGrid>
            <h:outputLabel value="Username: " style="font-weight:bold" />
            <h:inputText value="#{user.username}" />
            <h:outputLabel value="Password: " style="font-weight:bold" />
            <h:inputSecret value="#{user.password}" />
            <h:commandButton value="Submit" action="#{user.doLogin()}" />
        </h:panelGrid>
    </h:form>
</pre>

有人可以解释我为什么这会导致异常吗?这是我的第一个 JSF 应用程序。

ava.lang.NullPointerException
    at userSession.getUsername(userSession.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
4

1 回答 1

0

检查getUserName方法的第 45 行。可能该方法的参数有一个null引用,它。意味着一个 setter 给出一个空值。也尝试在doLogin()方法中获取您的输入文本

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map<String, String> parameterMap = (Map<String, String>) externalContext.getRequestParameterMap();
username=parameterMap.get("id_of_your_inputfield");
于 2013-03-02T16:08:33.750 回答