1

I am getting the below error for a simple login application that i developed. I have already provided Session Scopen in my managed bean and have my setters and getters available. Then why the error?

XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
</h:head>
<h:body>

<h:form>
<h2>Welcome </h2>
 <h:panelGrid columns="2">
<h:outputText value="Username" id="userName"></h:outputText>

<h:inputText id="user" value="#{login.credential.username}" /><h:outputText      value="Password" id="userPassword"></h:outputText>
<h:inputText id="password" value="#{login.credential.password}" />

</h:panelGrid>
    <h:commandButton action="success" value="Login" />
</h:form>
</h:body>
</html>

Login.java

    package com.practice;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped; 
    @ManagedBean(name="login")
    @SessionScoped
    public class Login
    {

        private Credential credential;

        public Login(){
            System.out.println("Login called");
        }

        public Credential getCredential() {
            return credential;
        }

        public void setCredential(Credential credential) {
            this.credential = credential;
        }

        public String loginProcess(){
            System.out.println("Method called");
            return "success";
        }

    }

Credential.java

    package com.practice;    
    import java.io.Serializable;
    public class Credential implements Serializable {

    private String username;
    private String password;
    public Credential(){
        System.out.println("Credential called");
    }

    public String getUsername() {
        System.out.println("Get username called");
        return username;
    }
    public void setUsername(String username) {
        System.out.println("Set username called");
        this.username = username;
    }
    public String getPassword() {
        System.out.println("Get password called");
        return password;
    }
    public void setPassword(String password) {
        System.out.println("Set  password called");
        this.password = password;
    }
}

Exception:

javax.servlet.ServletException: /index.xhtml @16,64 value="#{login.credential.username}": JBWEB006017: Target Unreachable, ''credential'' returned null
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
JBWEB000071: root cause

javax.el.PropertyNotFoundException: /index.xhtml @16,64 value="#{login.credential.username}": JBWEB006017: Target Unreachable, ''credential'' returned null
    com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    javax.faces.component.UIInput.validate(UIInput.java:960)
    javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    javax.faces.component.UIInput.processValidators(UIInput.java:698)
    javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    javax.faces.component.UIForm.processValidators(UIForm.java:253)
    javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1220)
    javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1164)
    com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)

Please help.

1 Answers1

0

In the code that you posted, the credential property is never initialised. Our do you call setCredential() from somewhere outside?

Try augmenting the constructor to initialise a default value (since otherwise credential is null by definition:

public Login(){
    System.out.println("Login called");
    credential = new Credential();
}

In case you do call setCredential() somewhere else, you might consider that you call it too late – maybe after the property is evaluated for display.

Jan D
  • 812
  • 5
  • 9