1

This post is continuity of Declaration of beans in applicationContext.xml

I have a small application using Spring 3 and Hibernate 4 with JSF2, When I am running the application I am getting.

java.lang.NullPointerException at
net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:53)

What am I doing wrongly to get nullpointer exception? Any help is highly appreciable.

ManagedBean:

@ManagedBean(name="empMB")
@Named
@Scope("request")
public class EmployeesManagedBean implements Serializable {

    public List<Employees> getEmpList() {
        try {
            empList = new ArrayList<Employees>();
            empList.addAll(getEmployeesService().getEmployees());
        } catch (Exception e) {
            e.printStackTrace();            
        }
        return empList;
    }
}

and I have Inject annotation:

@Inject
EmployeesService employeesService;

In EmployeesService I have annotations like:

@Named
public class EmployeesService implements IEmployeesService {

@Inject
EmployeesDAO employeesDAO;

@Override
public List<Employees> getEmployees() {
    return getEmployeesDAO().getEmployees();
}

In EmployeesDAO I have annotations like:

@Named
public class EmployeesDAO implements IEmployeesDAO {


@Override
public List<Employees> getEmployees() {
    List query = this.getSessionFactory().getCurrentSession().getNamedQuery("getEmp").list();               
    return query;
}

Entity class:

@Entity
@javax.persistence.NamedNativeQuery(name = "getEmp", query = "{ ? = call getemployees }", resultClass = Employees.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
@Table(name = "EMPLOYEES")
public class Employees {

and finally in applicationContext.xml I have

<context:component-scan base-package="net.test" />

Update 1:

When I use @Component("empMB") or @Named("empMB") I am getting the following exceptions

Error creating bean with name 'empMB': Injection of autowired 
dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: net.test.employees.service.EmployeesService
net.test.managed.bean.EmployeesManagedBean.employeesService; 
nested exception is org.springframework.beans.factory.
NoSuchBeanDefinitionException: No matching bean of type
[net.test.employees.service.EmployeesService] found for 
dependency: expected at least 1 bean which qualifies as autowire 
candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}

Update 2

applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

    <context:component-scan base-package="net.test" />
    <!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="oracle.jdbc" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@server:1521:DEV" />
        <property name="user" value="scott" />
        <property name="password" value="tiger" />
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean> 
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />     
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" />
    <!-- Transaction Manager is defined -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>
</beans>
Community
  • 1
  • 1
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 1
    any chance that `(EmployeesManagedBean.java:53` is inside constractor? if so , try to move it inside `@PostConstruct public void init() {....` method – Daniel Dec 10 '12 at 07:35
  • @Daniel I have added `getEmpList()` in my `ManagedBean` by updating my question. Do I need to put that in a constructor? – Jacob Dec 10 '12 at 07:41
  • 1
    I think you should remove the `@ManagedBean(name="empMB")` , cause you already using `@Named` , also try `@Named("empMB")` – Daniel Dec 10 '12 at 07:44
  • @Daniel So in that case you suggest to managedbean name `empMB` in `faces-config.xml`? – Jacob Dec 10 '12 at 07:47
  • 3
    If you use CDI (`@Named`) than don't use JSF (`@ManagedBean`) (and don't declare them in faces-config.xml either) take a look at http://stackoverflow.com/questions/10994158/difference-between-named-and-managedbean-annotations-in-jsf2-0-tomcat7 AND http://matthiaswessendorf.wordpress.com/2010/04/17/spring-3-0-and-jsr-330-part/ – Daniel Dec 10 '12 at 07:51
  • @Daniel If I am using @Named("empMB"), then I am getting exceptions, I have updated my question to include exceptions as Update 1. Thanks – Jacob Dec 10 '12 at 08:26
  • I'm not a CDI/Spring user , but you might need to define the classes in some XML ... take a look at this http://stackoverflow.com/questions/8961275/no-matching-bean-of-type-found-for-dependency – Daniel Dec 10 '12 at 08:32
  • 1
    You're attempting to manage a single bean class by a mix of three different frameworks JSF, CDI and Spring together! This makes no sense. Choose one and stick to it. For instance CDI's `@Inject` won't work at all on an instance which is provided by JSF's `@ManagedBean`. You'd need `@ManagedProperty` or `@EJB` instead (which in turn don't work at all in a Spring-managed instance, but that's nothing more than obvious). – BalusC Dec 10 '12 at 12:23
  • 1
    @Daniel Thanks for pointing out to replace `@ManagedBean` with `@Named`. – Jacob Dec 11 '12 at 05:44

2 Answers2

2

Try org.springframework.stereotype.Component annotation instead of ManagedBean, as

@Component("test")
class Test
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • If I would want to use Component annotation instead of ManagedBean, how can I specify bean name `empMB`? Could you provide an example? Thanks – Jacob Dec 10 '12 at 07:55
  • When I use @Component("empMB"), then I am getting exceptions, I have updated my question to include exceptions as Update 1. Thanks – Jacob Dec 10 '12 at 08:26
  • I guess the other beans need to have the @Component annotation too – Adriaan Koster Dec 10 '12 at 09:10
1

I have managed to resolve the issue of getting null pointer exception, courtesy to Marten Deinum

The mistake what I was doing was there were no @Inject for DAO class, I have modified my DAO class as

@Named
public class EmployeesDAO implements IEmployeesDAO {

    @Inject
    private SessionFactory sessionFactory;

    @Override
    public List<Employees> getEmployees() {

             List query = sessionFactory.getCurrentSession().getNamedQuery("getEmp").list();

        return query;
    }
}

and in ManagedBean I have made the changes as mentioned by Daniel to use @Named instead of @ManagedBean. Modified ManagedBean

@Named("empMB")
@Scope("request")
public class EmployeesManagedBean implements Serializable {

    @Inject
    IEmployeesService employeesService;

And of course added the following in applicationContext.xml to scan Entity class

<property name="annotatedClasses">  
          <list>
           <value>net.test.model.Employees</value>
           </list>  
           </property> 
Jacob
  • 14,463
  • 65
  • 207
  • 320