Spring框架JDBCTemplate NullPointerException

Spring框架JDBCTemplate NullPointerException,spring,jdbctemplate,Spring,Jdbctemplate,嗨,这个问题让我很困惑,我花了两天的时间在网上搜索,发现了许多类似的问题,但不幸的是,这些问题都已经解决了。这是我第一次问问题,因为我没有其他地方可以求助。我的背景我是自学成才的Java初学者,开始学习Spring框架 这是我的错误 线程主java.lang.NullPointerException中出现异常 在ie.cit.cloud.jdbctemplate.dao.InstitutionJdbcTemplate.getInstitution InstitutionJdbcTemplate.

嗨,这个问题让我很困惑,我花了两天的时间在网上搜索,发现了许多类似的问题,但不幸的是,这些问题都已经解决了。这是我第一次问问题,因为我没有其他地方可以求助。我的背景我是自学成才的Java初学者,开始学习Spring框架 这是我的错误 线程主java.lang.NullPointerException中出现异常 在ie.cit.cloud.jdbctemplate.dao.InstitutionJdbcTemplate.getInstitution InstitutionJdbcTemplate.java:58 位于ie.cit.cloud.App.mainApp.java:134

main中有问题的代码行被注释

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;


}
我不明白为什么在前面的两个方法createInstitution、deleteInstitution中会给我一个NullPointerException,插入一个删除数据都可以,没有问题

我的InstitutionMapper课程如下

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;
}

我真的很想得到一些关于这个问题的指导,因为我已经搜索了网络,尝试了不同的解决方案,但无论我使用什么,我似乎都无法让数据返回,即使使用命令行提示符也可以。

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

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

@Override
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
这个方法曾经被调用过吗?不可以。您的主要任务是:

InstitutionJdbcTemplate ins = new InstitutionJdbcTemplate();
List<InstitutionJdbcTemplate> inst = (List<InstitutionJdbcTemplate>) ins.getInstitution("_CIT");

阅读

发布您的stacktrace。谁将jdbcTemplate和数据源注入到您的DAO中?同时发布应用程序上下文。
InstitutionJdbcTemplate ins = appContext.getBean(InstitutionJdbcTemplate.class);