Spring Introductory annotations throwing nullpointer exception

This post is the continuity of the beans declaration in applicationContext.xml

I have a small application using Spring 3 and Hibernate 4 with JSF2. When I launch the application that I receive.

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

What am I doing wrong to get a nullpointer exception ? Any help is very noticeable.

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 an 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 get 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> 
+1
source share
2 answers

I managed to solve the problem of getting a null pointer exception by kindly providing Marten Deinum

The error in what I was doing was not @Inject for the DAO class, I changed 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 made the changes Daniel mentioned to use @Named instead of @ManagedBean . Modified ManagedBean

 @Named("empMB") @Scope("request") public class EmployeesManagedBean implements Serializable { @Inject IEmployeesService employeesService; 

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

 <property name="annotatedClasses"> <list> <value>net.test.model.Employees</value> </list> </property> 
+1
source

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

 @Component("test") class Test 
+2
source

All Articles