我有一個NullPointerException,它正在竊聽我。在我的粗略研究中,我發現這通常發生在人們沒有自動裝配他們的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>
非常感謝!
在至極線你有沒有空指針異常? – 2014-12-05 14:24:59
對不起,它在我的服務中,調用ReportDaoImpl中的getReportAccounts()方法。如果有幫助,我也可以發佈服務。 – iamthereplicant 2014-12-05 14:27:23
請發佈堆棧跟蹤,哪一行是NPE? – kamoor 2014-12-05 14:33:36