1

I have : - Repository Class:

@SessionScoped
public class EmployeeRepository {
    @PersistenceContext
    EntityManager entityManager;
    public List<Employee> getEmployees(){
        TypedQuery<Employee> qu = entityManager.createQuery("select * from Employee", Employee.class);
        List<Employee> emp2 = qu.getResultList();
        return emp2;
    }
}

and

Managed Bean:

@ManagedBean(name = "helloWorldBean")
public class HelloWorldBean {
    @Inject
    private EmployeeRepository employeerepo;
    public String getMsg() {
        return "Hallo";
    }
    public String getEmployees() {
        return String.valueOf(employeerepo.getEmployees().size());
    }
}

and a JSF Page:

<h:head>
    <title>JavaCodeGeeks</title>
</h:head>
<h:body>
    - Message : <h:outputText value="#{helloWorldBean.msg}" />
    - Employee count : <h:outputText value="#{helloWorldBean.employees}" />
</h:body>
</html>

I have an beans.xml in my META-INF Folder (src\META-INF) without special configurations:

<?xml version="1.0"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="all" version="1.1">
</beans>

Problem:

Page throws nullpointer exception, because the EmployeeRepository is not injected in the HelloWorldBean.

What to do to be able to inject Instances of classes in my case ?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
mcfly soft
  • 11,289
  • 26
  • 98
  • 202

1 Answers1

5

As you are using CDI, you should not be using @ManagedBean (which is a JSF annotation). While I have actually seen this working, most implementations will not allow you to inject a CDI bean into a classic JSF bean.

To allow for CDI injection in JSF beans, the CDI specification allows you to define JSF backing beans by specifying the @Named annotation in combination with a scope (@RequestScoped, @javax.faces.view.ViewScoped, @SessionScoped and @ApplicationScoped).

So in summary, the following piece of code should solve your problem,

@Named
@RequestScoped
public class HelloWorldBean {
    @Inject
    private EmployeeRepository employeerepo;
    public String getMsg() {
        return "Hallo";
    }

    public String getEmployees() {
        return String.valueOf(employeerepo.getEmployees().size());
    }
}

You can also read up further on the topic via some old Q/A's here on the site,

How to inject a CDI Bean in a ManagedBean?

@ManagedProperty does not work in a CDI managed bean

CDI beans injection

Adam Waldenberg
  • 2,271
  • 9
  • 26
  • Thanks for helping. With the new code it seems that the xhtml does not find my bean. The HelloWorldBean is not called. Any help ? – mcfly soft Jan 10 '19 at 06:53
  • @mcflysoft Make sure you call `helloWorldBean` and not `HelloWorldBean`. Any class name will get a variable with camel-case notation (unless you explicitly specify what to name it). Also make sure you run your application in an environment that fully supports CDI. Tomcat 7 doesn't out of the box and needs additional configuration. I would suggest you look into a full fledged JavaEE server with up-to-date implementations of JavaEE / JakartaEE like Payara, GlassFish or WildFly. – Adam Waldenberg Jan 10 '19 at 07:29
  • Or TomEE ;-) Yes, effectively this is a new question fpr which there are several related q/an stackoverflow – Kukeltje Jan 10 '19 at 07:42
  • @Kukeltje Is TomEE fully up to date these days? Last time I tried it I had to muck around and do a lot of extra configuraiton steps to get everything working properly. – Adam Waldenberg Jan 10 '19 at 07:54
  • Last time I tried it worked sort of ok, but that was just a try without jms but with cdi, jsf, ejb AND 1.5 years ago. I usually use Wildfly. – Kukeltje Jan 10 '19 at 08:06