0

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
    }       



}
Jenna Deng
  • 31
  • 1
  • 10
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Mahendran Ayyarsamy Kandiar Nov 30 '15 at 18:53
  • 2
    Line 45 in `Login` is the culprit which you did not mention. `@Named` and `@Stateless` are mutually exclusive. You should not be using them together. (EJBs (local or remote) need not explicitly implement the `java.io.Serializable` interface. They are already serializable as they use serializable proxies). `eager = true` is completely useless in the example given as it is only meant for application scoped JSF managed beans. – Tiny Nov 30 '15 at 19:00
  • @Tiny Thanks so should I fix this by removing either `@Named and `@Stateless in the DAO class? – Jenna Deng Nov 30 '15 at 19:13
  • @Tiny Ya I tried removing the `@Named and I'm still getting the same null pointer exception crashing after line 45 :-( – Jenna Deng Nov 30 '15 at 19:43
  • Some object remains `null` in `Login.login()` at line 45. Mentioning where exactly that happens might help better. Most probably it appears to be `credentials` being retrieved from `SessionMap` but it is merely a guess. There could be problems at injection points in the managed bean as well. – Tiny Nov 30 '15 at 19:48
  • @Tiny Thanks I've edited my post to include that it `credential` had values as `name` and `pass` and `authenticate` is null. But would authenticate being null be a problem? – Jenna Deng Nov 30 '15 at 20:01
  • `authenticate` is an EJB which should be injected normally unless there is something wrong in the application configuration. Does injecting the EJB using `@EJB` help? Further, you are already using CDI. Therefore, you can use CDI managed beans (`@Named`, `javax.enterprise.context.SessionScoped`) avoiding JSF managed beans. A session scoped bean, in this case, is unnecessarily verbose for things like login. A view scoped bean should suffice adequately `javax.faces.view.ViewScoped` in case of CDI beans. – Tiny Dec 01 '15 at 08:15

1 Answers1

0

I found where the bug was. It was because I had @Mappedsuperclass and @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) at the same time on one of my entity classes.

Jenna Deng
  • 31
  • 1
  • 10