在Spring中使用jdbcTemplate的简单示例时,空指针异常

时间:2014-02-06 11:50:00

标签: java spring spring-jdbc jdbctemplate

我正在用Spring学习JdbcTemplate,当我运行我的java应用程序时,我在这一行得到空指针:

return jdbcTemplate.queryForMap(sql, id);

请在下面找到全班:

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);
    }

请在下面找到我的SpringContext文件:

<?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>

请在下面找到我的主要方法:

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);

    }
}

请在eclipse控制台中找到我的异常:

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)

知道为什么它是空的吗?

提前感谢您提供任何帮助

3 个答案:

答案 0 :(得分:2)

将setDataSource更改为:

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

答案 1 :(得分:1)

您永远不会初始化jdbcTemlpate实例变量。在这种情况下,它被初始化为null并且你得到NullPointerException(你不能在null上调用方法)。您需要自己初始化jdbcTemplate

引用Java Language Specification

  

每个类变量,实例变量或数组组件都是   在创建时使用默认值初始化[...]为所有人   引用类型,默认值为null。

答案 2 :(得分:1)

您可以在DAOImpl类中扩展org.springframework.jdbc.core.support.JdbcDaoSupport并使用继承的getJdbcTemplate()方法来获取jdbcTemplate。在这种情况下,您可以从类中删除变量jdbcTemplate