Search code examples
jsfjakarta-eeprimefacesjsf-2dependency-injection

Why the injection of Session beans gives null pointer exception?


I have a very simple JSF 2.2 ( hibernate 4.3 ) application that has 2 pages. first page is login.xhtml which is bound to below session bean

import javax.inject.Named;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;

import javax.faces.context.FacesContext;


@Named(value = "loginBean")
@SessionScoped
public class LoginBean implements Serializable {

    /**
     * Creates a new instance of LoginBean
     */
    Company company;
   
    private static final long serialVersionUID = 1520318172495977648L;

    public String Login() {
        CompanyHelper companyHelper = new CompanyHelper();
        company = companyHelper.Login(this.loginName, this.password);
        if (company != null) {
            return "";
        } else {
            FacesMessage msg = new FacesMessage("Failed", "Wrong Usernames or password.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "";
        }
    }


}

It just validate the Username and password from the DB and returns the company object

the second page delivery.xhtml which bound to view bean. Inside this bean I have injected the login bean but it returns null every time I use Company object. However, when I go to login.xhtml I found that company object is not null. Below is the delivery bean.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.model.SelectItem;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;


@Named(value = "deliveryBean")
@ViewScoped
public class DeliveryBean implements Serializable {

    /**
     * Creates a new instance of DeliveryBean
     */
    @Inject
    private LoginBean loginBean;


    @PostConstruct
    public void init() {
       
        // returns null pointer exception
        Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, loginBean.company);
       

    }

    public String SendItem() {
        // reutn null point exception 
        String personName=deliverRequest.setDeliverFrom(loginBean.company.getContactPersonName());
        return "";
    }

  

}

here is the pom.xml

 <dependencies>
        <!-- https://mvnrepository.com/artifact/org.primefaces/primefaces -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.1</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>


      

     
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.11.Final</version>
        </dependency>

 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>

I am deploying the application on Glassfish 4.1


Solution

  • @Inject
    private LoginBean loginBean;
    

    The way injection works is first it creates a proxy object in the loginBean field. CDI doesn't create or lookup a LoginBean instance immediately. Instead it waits until you call a method on the proxy in the loginBean field. CDI calls this a contextual reference - the instance that you'll get depends on the context in which you asked for it.

    But you don't call a method:

    loginBean.company.toString()
    

    You access the company field directly - CDI can't intercept this. So you get a useless null value from the proxy.

    The solution is to not access managed beans' fields directly. Instead make company private and create a getter:

    public class LoginBean implements Serializable {
    
    private Company company;
    
    public Company getCompany() {
        return company;
    }
    

    And call this getter in the DeliveryBean.