When login is pressed on the loginpage.xhtml the commandbutton calls a login bean which calls a DAO class that checks the credentials with a table stored in a SQL server. I've been getting the following null pointer exception when I press login. Does anyone have any ideas as to how I can fix this? Any help would be greatly appreciated. When I set break point I noticed that it goes in to the code for throwing exceptions after line 45 in the login bean, however, the variables at line 45 have values ( username, password) but authentication is null.
javax.faces.el.EvaluationException: javax.el.ELException: /view/LoginPage.xhtml at line 54 and column 77 action="#{login.login}": java.lang.NullPointerException
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100)
at javax.faces.component.UICommand.broadcast(UICommand.java:120)
at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:995)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:278)
at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1307)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:733)
at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:172)
at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:119)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1232)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:781)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:480)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1114)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:940)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881)
Caused by: javax.el.ELException: /view/LoginPage.xhtml at line 54 and column 77 action="#{login.login}": java.lang.NullPointerException
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:95)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88)
... 31 more
Caused by: java.lang.NullPointerException
at com.cfins.esbConsole.beans.Login.login(Login.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
at java.lang.reflect.Method.invoke(Method.java:620)
at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83)
... 32 more
The loginPage.xhtml code
<body class="mainBody">
<div id="myId" class="createUser" style="border-color: #66ff66; border-radius: 7px;" align="center">
<fieldset class="createUser">
<legend align="top">
<font>Enter Credentials</font>
</legend>
<h:form prependId="false">
<br></br>
<h:panelGrid columns="3" >
<h:outputLabel styleClass="loginFont" for="username" >Username:</h:outputLabel>
<h:inputText styleClass="inputFields" id="username" value="#{credentials.username}" required="true" requiredMessage="* Username is mandatory."/><h:message for="username" styleClass="myErrorFormat"/>
<h:outputLabel styleClass="loginFont" for="password">Password:</h:outputLabel>
<h:inputSecret styleClass="inputFields" id="password" value="#{credentials.password}" required="true" requiredMessage="* Password is mandatory."/><h:message for="password" styleClass="myErrorFormat"/>
<h:outputLabel styleClass="loginFont" for="environment">Environment:</h:outputLabel>
</h:panelGrid>
<h:commandButton value="Login" class="button" action="#{login.login}"/>
</h:form>
<h:inputHidden id="userValidation"/>
<h:message for="userValidation" styleClass="myErrorFormat"/>
<br/> <br/>
</fieldset>
</div>
</body>
And this is the login bean
@ManagedBean(name = "login", eager = true)
@SessionScoped
public class Login implements Serializable {
@Inject
private AuthenticationDao authenticate;
@Inject
private TransactionsDao transDao;
private UsrDetailT loggedIn = null;
private static final long serialVersionUID = 7965455427888195913L
public String login() throws Exception{
Credentials credentials = (Credentials) FacesContext
.getCurrentInstance().getExternalContext().getSessionMap()
.get("credentials");
String name = credentials.getUsername();
String pass = credentials.getPassword();
UsrDetailT ud = authenticate.validateUser(name, pass);
if (ud != null) {
this.setLoggedIn(ud);
transDao.setEsbDB(credentials.getEnvironment());
return "success";
} else {
FacesMessage message = new FacesMessage("Invalid Credentials. Please enter your Username and Password again.");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage("userValidation", message);
return "fail";
}
//other methods
}
}