首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring框架JDBCTemplate NullPointerException

Spring框架JDBCTemplate NullPointerException
EN

Stack Overflow用户
提问于 2014-10-24 13:04:11
回答 1查看 6.5K关注 0票数 0

嗨,这个问题让我很困惑,我花了两天时间在网上搜索,发现许多类似的问题已经解决了,不幸的是,这些问题对mine.So没有用。这是我第一次问一个问题,因为我没有mine.So的背景,我在turn.My的入门阶段是自学的,这是我在ie.cit.cloud.App.main(App.java:134)的InstitutionJdbcTemplate.java:58线程中的错误例外。

对main中的违规代码行进行了注释。

代码语言:javascript
复制
public class App{
    private static ApplicationContext appContext;

     public static void main(String[] args){

        appContext = new ClassPathXmlApplicationContext("configuration.xml");

        InstitutionJdbcTemplate ins = new InstitutionJdbcTemplate();

        ***//App.java:134 below***
        List<InstitutionJdbcTemplate> inst = (List<InstitutionJdbcTemplate>) ins.getInstitution("_CIT");
    }
}

 The offending line of code in InstitutionJdbcTemplate is also commented below
public class InstitutionJdbcTemplate extends Institution implements InstitutionDAO {

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

@Override
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplateObject = new JdbcTemplate(dataSource);

}

@Override
public void createInstitution(String instId, String name, String address) {
    String SQL = "insert into Institution (inst_id, name, address) values (?, ?, ?)";
    jdbcTemplateObject.update(SQL, new Object[] { instId, name, address});
    System.out.println("Created Record Name = " + instId + " Name = " + name);
    return;

}

@Override
public void deleteInstitution(String instId) {
    String SQL = "delete from Institution where inst_id = ?";
    jdbcTemplateObject.update(SQL, new Object[] {instId});
    System.out.println("Deleted Record with ID = " + instId );
    return;

}

@Override
public void updateInstitution(String name,String address,String instId) {
    String SQL = "UPDATE institution SET name = ?, address = ? WHERE inst_id = ?";
    jdbcTemplateObject.update(SQL, name, address,instId);
    System.out.println("Updated Record with ID = " + instId );
    return;

}

@Override
public Institution getInstitution(String inst) {
        String SQL = "select *  from Institution where inst_id= ?";
    ***//line 58 below***
    Institution instCIT = (Institution) jdbcTemplateObject.queryForObject(SQL, 
                    new Object[]{inst}, new InstitutionMapper());
    return instCIT;


}

我不明白为什么会给我一个NullPointerException,因为在前面的两个方法(createInstitution、deleteInstitution)中,所有这些方法都能很好地插入删除数据--没有问题。

我的InstitutionMapper类如下所示

代码语言:javascript
复制
public class InstitutionMapper implements RowMapper<Institution>{

@Override
public Institution mapRow(ResultSet rs, int rowNum) throws SQLException {
    Institution inst = new Institution();
    String str= rs.getString("inst_id");
    inst.setInsId(InstitutionId.valueOf(str));
    inst.setName(rs.getString("name"));
    inst.setAddress(rs.getString("address"));
    return inst;
}

我非常希望在这个问题上提供一些指导,因为我已经搜索过网络,尝试了不同的解决方案,但是无论我使用什么,我似乎都无法让我的数据返回,即使它在那里使用命令行提示符。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-24 13:12:22

非常简单。在第58行,您正在jdbcTemplateObject上调用一个方法。

那么,这个变量在哪里初始化为null以外的变量呢?

代码语言:javascript
复制
@Override
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}

这个方法有调用过吗?不是的。您的主要功能如下:

代码语言:javascript
复制
InstitutionJdbcTemplate ins = new InstitutionJdbcTemplate();
List<InstitutionJdbcTemplate> inst = (List<InstitutionJdbcTemplate>) ins.getInstitution("_CIT");

因此,由于setDataSource()从未在新的InstitutionJdbcTemplate对象上调用,所以变量为null,调用null上的方法会抛出NPE。

您的意图可能是setDataSource()方法应该由Spring调用。这只有在Spring实例化InstitutionJdbcTemplate而不是您的情况下才有可能。通过执行new InstitutionJdbcTemplate(),您完全忽略了Spring。您所做的只是创建一个对象,Spring无法知道这一点。因此,假设您在Spring配置中定义了一个类型为InstitutionJdbcTemplate的bean,那么您需要做的就是从Spring上下文中获取这个bean:

代码语言:javascript
复制
InstitutionJdbcTemplate ins = appContext.getBean(InstitutionJdbcTemplate.class);

读取http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/htmlsingle/#beans-factory-client

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26548464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档