Spring空指针异常

Spring空指针异常,spring,jdbctemplate,Spring,Jdbctemplate,我有一个NullPointerException,它让我非常恼火。在我对SO的粗略研究中,我发现这通常发生在人们没有自动连接他们的jdbctemplate时,但据我所知,应该正确连接。作为一名主管,我仍在学习Spring的基础知识,我正在使用的代码是遗留项目的一部分 ReportDaoImpl @Service public class ReportDaoImpl implements ReportDao { @Autowired JdbcTemplate jdbcTem

我有一个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;
             }
        }

}
@服务
公共类ReportDaoImpl实现ReportDao{
@自动连线JdbcTemplate JdbcTemplate;
私有静态最终记录器log=Logger.getLogger(ReportDaoImpl.class);
private static final String SELECT_ALL_ACCOUNT_INFO=“SELECT acct_name,login_name,pswd FROM PG_PAYPAL_ACCOUNTS”;
@自动连线
public ReportDaoImpl(数据源数据源)
{
log.debug(“尝试构建”);
jdbcTemplate=新的jdbcTemplate(数据源);
日志调试(“构建完成”);
}
@凌驾
公共阵列列表getReportAccounts(){
log.debug(“查找帐户”);
列表结果列表;
字符串[]帐户详细信息;
ArrayList accounts=新的ArrayList();
试一试{
log.debug(“执行查询”);
resultList=jdbcTemplate.queryForList(选择所有账户信息);
log.debug(“查询结果”);
log.debug(resultList.toString());
if(resultList!=null&&resultList.size()>0){
用于(映射临时行:结果列表){
debug(“将查询结果映射到帐户POJO”);
accountDetails=新字符串[3];
accountDetails[0]=(字符串)temprow.get(“账户名称”);
accountDetails[1]=(字符串)temprow.get(“登录名”);
accountDetails[2]=(字符串)temprow.get(“pswd”);
log.debug(“单个帐户详细信息”);
log.debug(accountDetails.toString());
调试(“将单个帐户添加到帐户数组”);
账户。添加(账户详情);
}
}
归还账户;
}捕获(例外e){
log.debug(“无结果:+e);
System.out.println(“无结果:+e”);
返回null;
}
}
}
XML



非常感谢

这个问题很难理解。您执行的是什么,它实际上生成了NullPointerException

就我对您的问题的理解而言,您可能在Spring中有一个常见错误:如果您的变量jdbcTemplate是在ReportDaoImpl类中自动连接的,那么这个类也必须通过自动连接而不是手动实例化来创建。数据源也会发生同样的情况

这意味着:

ReportDaoImpl reportDaoImp = new ReportDaoImpl(dataSource);
不会有一个已安装的数据源(非实例化的jdbcTemplate),因为实际上是您在进行实例化,而不是让Spring进行实例化

因此,您需要在应用程序类中为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

编辑:
实际上,这个问题的答案可能会回答您的问题:

在哪一行您有NullPointerException吗?对不起,它在我的服务中,在ReportDaoImpl中调用getReportAccounts()方法。如果有帮助的话,我也可以发布服务。请发布堆栈跟踪,NPE在哪一行?是的,所以我没有在上下文中传递:/谢谢链接,我会给你一个答案。
@Autowired
ReportDaoImpl reportDaoImp;