Ankur Mukherjee
Ankur Mukherjee

Reputation: 3867

Null pointer exception while accessing bean in java for already referred bean Spring mvc

Here is the code snippet for my dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.*"></context:component-scan>
    <context:annotation-config />
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean name="viewResolver" id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean name="dataSource" id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/com"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="sessionFactoryBean" name="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
            </list>
        </property>
    </bean>
    <bean id="template" name="template" class="org.springframework.orm.hibernate3.HibernateTemplate" autowire-candidate="true">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <bean id="EmployeeDao" name="EmployeeDao" class="com.dao.EmployeeDaoImpl">
        <property name="template" ref="template"></property>
    </bean>
</beans>

And here is the code snippet from my EmployeeDaoImpl

public class EmployeeDaoImpl  implements EmployeeDao{
    HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }
    public boolean saveDummyEmployees(List<Employee> employees) {
        try{
            template.saveOrUpdateAll(employees);
            return true;
        }catch(DataAccessException ex){ 
            ex.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

this line template.saveOrUpdateAll(employees); is having null pointer exception corresponding to the template.

Please let me know what error I am making.

Upvotes: 0

Views: 157

Answers (1)

Matteo Baldi
Matteo Baldi

Reputation: 5828

In your FrontController class you should autowire (or get from application context) EmployeeDao.

Upvotes: 1

Related Questions