簡體   English   中英

在Spring中使用jdbcTemplate的簡單示例時出現空指針異常

[英]Null pointer exception while using simple example of jdbcTemplate in Spring

我正在用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)

知道為什么它為null嗎?

預先感謝您的任何幫助

將setDataSource更改為:

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

您永遠不會初始化jdbcTemlpate實例變量。 在這種情況下,它被初始化為null,並且您將獲得NullPointerException (您不能在null上調用方法)。 您需要自己初始化jdbcTemplate

引用Java語言規范

創建時,每個類變量,實例變量或數組組件均使用默認值初始化。對於所有引用類型,默認值為null。

您可以在DAOImpl類中擴展org.springframework.jdbc.core.support.JdbcDaoSupport ,並使用繼承的getJdbcTemplate()方法獲取jdbcTemplate。 在這種情況下,您可以從類中刪除變量jdbcTemplate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM