简体   繁体   中英

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

Change setDataSource to:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM