user1999453
user1999453

Reputation: 1423

Null pointer exception while using simple example of jdbcTemplate in Spring

I am learning JdbcTemplate with Spring and when i am running my java application i am getting null pointer on this line:

return jdbcTemplate.queryForMap(sql, id);

Please find the whole class below:

public class CustomerDaoImpl implements CustomerDAO{

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
      public void setDataSource(DataSource ds) {
        dataSource = ds;
      }

    public Map getCustomerById(String id) {
        String sql = "SELECT * FROM CUSTOMER WHERE CUST_ID = ?";

        return jdbcTemplate.queryForMap(sql, id);
    }

Please find my SpringContext file below:

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Scans within the base package of the application for @Components to 
        configure as beans -->

    <context:property-placeholder location="classpath:db.properties" />


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="customerDAO" class="com.tuto.dao.impl.CustomerDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>


</beans>

Please find my Main method below:

public class JdbcTemplateApp {
    public static void main(String[] args) {
        // if you have time,
        // it's better to create an unit test rather than testing like this :)

        ApplicationContext context = new ClassPathXmlApplicationContext(
                "integration.xml");

        CustomerDAO customerDAO = (CustomerDAO) context.getBean("customerDAO");

        Map customerA = customerDAO.getCustomerById("1");
        System.out.println("Customer A : " + customerA);

    }
}

Please find below my exception in eclipse console:

INFO: Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
Exception in thread "main" java.lang.NullPointerException
    at com.tuto.dao.impl.CustomerDaoImpl.getCustomerById(CustomerDaoImpl.java:23)
    at com.tuto.main.JdbcTemplateApp.main(JdbcTemplateApp.java:23)

Any idea why it is null please?

Thanks in advance for any help

Upvotes: 0

Views: 9541

Answers (3)

Nigel Tufnel
Nigel Tufnel

Reputation: 11524

You never initialize jdbcTemlpate instance variable. In this case it's initialized as null and you're getting NullPointerException (you can't call methods on null). You need to initialize jdbcTemplate by yourself.

To quote Java Language Specification:

Each class variable, instance variable, or array component is initialized with a default value when it is created [...] For all reference types the default value is null.

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

You can extend org.springframework.jdbc.core.support.JdbcDaoSupport in your DAOImpl Class and use inherited getJdbcTemplate() method to get the jdbcTemplate. in this case you can remove the variable jdbcTemplate from your class

Upvotes: 1

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22496

Change setDataSource to:

public void setDataSource(DataSource ds) {
    jdbcTemplate = new JdbcTemplate(ds);
}

Upvotes: 2

Related Questions