Results 1 to 10 of 14

Thread: Getting nullpointer exception when Annotation are used for Spring 3

Threaded View

  1. #1
    Join Date
    Dec 2012
    Posts
    10

    Default Getting nullpointer exception when Annotation are used for Spring 3

    Hello

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

    Code:
      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.

    Thanks

    ManagedBean:


    Code:
    @Named("empMB")
        @Scope("request")
        public class EmployeesManagedBean implements Serializable {
    
        @Inject
        IEmployeesService employeesService;
        
            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:


    In EmployeesService I have annotations like:
    Code:
        @Transactional(readOnly = true)
        @Named
        public class EmployeesService implements IEmployeesService {
    
        @Inject
        IEmployeesDAO employeesDAO;
        
        @Override
        public List<Employees> getEmployees() {
            return getEmployeesDAO().getEmployees();
        }
    In EmployeesDAO I have annotations like:

    Code:
        @Named
        public class EmployeesDAO implements IEmployeesDAO {
       
    
        @Override
        public List<Employees> getEmployees() {
            List query = this.getSessionFactory().getCurrentSession().getNamedQuery("getEmp").list();                
            return query;
        }
    Entity class:

    Code:
        @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

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

    applicationContext.xml:

    Code:
        <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/schem...-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schem...ing-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>
    Stacktrace
    Code:
    at net.test.employees.dao.EmployeesDAO.getEmployees(EmployeesDAO.java:27)
    	at net.test.employees.service.EmployeesService.getEmployees(EmployeesService.java:24)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    	at $Proxy137.getEmployees(Unknown Source)
    	at net.test.managed.bean.EmployeesManagedBean.getEmpList(EmployeesManagedBean.java:51)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    Last edited by pol; Dec 10th, 2012 at 06:02 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •