简体   繁体   English

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

[英]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: 我正在用Spring学习JdbcTemplate,当我运行我的Java应用程序时,我在这条线上得到了空指针:

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: 请在下面找到我的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>

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: 请在下面的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)

Any idea why it is null please? 知道为什么它为null吗?

Thanks in advance for any help 预先感谢您的任何帮助

Change setDataSource to: 将setDataSource更改为:

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

You never initialize jdbcTemlpate instance variable. 您永远不会初始化jdbcTemlpate实例变量。 In this case it's initialized as null and you're getting NullPointerException (you can't call methods on null ). 在这种情况下,它被初始化为null,并且您将获得NullPointerException (您不能在null上调用方法)。 You need to initialize jdbcTemplate by yourself. 您需要自己初始化jdbcTemplate

To quote Java Language Specification : 引用Java语言规范

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. 创建时,每个类变量,实例变量或数组组件均使用默认值初始化。对于所有引用类型,默认值为null。

You can extend org.springframework.jdbc.core.support.JdbcDaoSupport in your DAOImpl Class and use inherited getJdbcTemplate() method to get the jdbcTemplate. 您可以在DAOImpl类中扩展org.springframework.jdbc.core.support.JdbcDaoSupport ,并使用继承的getJdbcTemplate()方法获取jdbcTemplate。 in this case you can remove the variable jdbcTemplate from your class 在这种情况下,您可以从类中删除变量jdbcTemplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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