Spring Null Pointer Exception

时间:2014-12-05 14:22:33

标签: spring jdbctemplate

我有一个NullPointerException,这让我感到害怕。在我对SO的粗略研究中我发现,当人们没有自动装配他们的jdbctemplate时,通常会发生这种情况,但据我所知,应该正确接线。作为一个负责人,我仍然在学习Spring的基础知识,而我正在使用的代码是遗留项目的一部分。

ReportDaoImpl

    @Service
    public class ReportDaoImpl implements ReportDao {
    @Autowired JdbcTemplate jdbcTemplate;
    private static final Logger log = Logger.getLogger(ReportDaoImpl.class);

        private static final String SELECT_ALL_ACCOUNT_INFO = "SELECT acct_name, login_name, pswd FROM PG_PAYPAL_ACCOUNTS";

        @Autowired
        public ReportDaoImpl(DataSource dataSource)
        {
            log.debug("attempt building");
            jdbcTemplate = new JdbcTemplate(dataSource);
            log.debug("building complete");
        }

        @Override
        public ArrayList<String[]> getReportAccounts() {
             log.debug("looking for accounts");
             List<Map<String, Object>> resultList;
             String[] accountDetails; 
             ArrayList<String[]> accounts = new ArrayList<String[]>();
             try{
                 log.debug("Excecuting Query");
                 resultList = jdbcTemplate.queryForList(SELECT_ALL_ACCOUNT_INFO);
                 log.debug("Query Results");
                 log.debug(resultList.toString());
                 if(resultList != null && resultList.size() > 0){
                     for(Map<String, Object> temprow: resultList){
                         log.debug("Mapping Query Results to Account POJO");
                         accountDetails = new String[3];
                         accountDetails[0] = (String) temprow.get("acct_name");
                         accountDetails[1] = (String) temprow.get("login_name");
                         accountDetails[2] = (String) temprow.get("pswd");
                         log.debug("Single account details");
                         log.debug(accountDetails.toString());
                         log.debug("Adding single account to accounts array");
                         accounts.add(accountDetails);
                     }
                 }
                 return accounts;
             } catch (Exception e){
                 log.debug("NO RESULTS: " + e);
                 System.out.println("NO RESULTS: " + e);
                 return null;
             }
        }

}

XML

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

<context:annotation-config/>
<context:component-scan base-package="org.test.testpackage.report" annotation-config="false" />

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

<import resource="classpath:db-config.xml"/>

<bean id="pgReportService" name="pgReportService" class="org.test.testpackage.report.service.AccountLookup" scope="singleton" />

<bean id="jdbcTemplate" name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

非常感谢!

1 个答案:

答案 0 :(得分:0)

这个问题很难读懂。你在执行什么实际上是在生成NullPointerException?

根据我对你的问题的理解,你可能在Spring中遇到一个常见的错误:如果你的变量jdbcTemplate在ReportDaoImpl类中自动装配,那么这个类也必须通过自动装配而不是通过手动实例化来创建。 DataSource也是如此。

这意味着:

ReportDaoImpl reportDaoImp = new ReportDaoImpl(dataSource);

不会有一个instatiated dataSource(非实例化的jdbcTemplate),因为实际上是实际进行实例化而不让Spring执行它。

因此,您需要在Application类中为ReportDaoImpl指定一个bean,例如:

  @Autowired
    public ReportDaoImpl(DataSource dataSource){
        ReportDaoImpl reportDaoImp = new ReportDaoImp();
        reportDaoImp.setDataSource(dataSource);
        return reportDaoImp;
    }

在使用ReportDaoImp的类中定义属性:

@Autowired
ReportDaoImpl reportDaoImp;

这将实例化DataSource(如果还定义了DataSource的bean),然后实例化传递此DataSource实例的ReportDaoImpl。

编辑: 实际上这个问题的答案可能会回答你的问题:Why is my Spring @Autowired field null?

相关问题