<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)
答案 0 :(得分:0)
检查getUserName
方法的第45行。可能该方法的参数具有null
引用,其中。表示setter给出null。还可以尝试使用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");