1

I've got a NullPointerException that is bugging the hell out of me. In my cursory research on SO I've found that this is usually happening when people are not autowiring their jdbctemplate, but as far as I can tell that should be wiring correctly. As a heads up I'm still learning the basics of Spring, and the code I'm working with is part of a legacy project.

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>

Thanks so much!

kenju
  • 5,866
  • 1
  • 41
  • 41
iamthereplicant
  • 257
  • 1
  • 4
  • 13

1 Answers1

0

The question is hard to read. What are you executing that is actually generating a NullPointerException?

For what I understand of your question you might be having a common mistake in Spring: If your variable jdbcTemplate is autowired in the ReportDaoImpl class then this class must be created also by autowiring and not by manual instantiation. Same thing happens for DataSource.

This means:

ReportDaoImpl reportDaoImp = new ReportDaoImpl(dataSource);

will not have an instatiated dataSource (non instantiated jdbcTemplate), because it is you who is actually making the instantiation and not letting Spring to do it.

So you need to specify a bean for ReportDaoImpl in your Application class, such as:

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

And in the class where you use ReportDaoImp define an attribute:

@Autowired
ReportDaoImpl reportDaoImp;

This will instantiate DataSource (if the a bean for DataSource is also defined) and then instantiate a ReportDaoImpl passing this instance of DataSource.

Edit: Actually this question answers might answer yours: Why is my Spring @Autowired field null?

Community
  • 1
  • 1
Javierfdr
  • 1,122
  • 1
  • 14
  • 22