Reputation: 3
Hi this problem has me perplexed and having spent two days searching the net and found many similar problems which have been resolved unfortunately hasn't worked for mine.So this is my first time asking a question as I have no where else to turn.My Background I am self taught beginner at Java beginning the Spring framework This is my error Exception in thread "main" java.lang.NullPointerException at ie.cit.cloud.jdbctemplate.dao.InstitutionJdbcTemplate.getInstitution (InstitutionJdbcTemplate.java:58) at ie.cit.cloud.App.main(App.java:134)
The offending line of code in main is commented
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;
}
I don't understand why I have been given a NullPointerException as in the two previous methods (createInstitution,deleteInstitution) all work fine inserting an deleting data no problems.
My InstitutionMapper class is as follows
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;
}
I really would appreciate some guidance on this issue as I have scoured the net trying different solutions but whatever I use I just cant seem to get my data to come back out even though it is there using the command line prompt.
Upvotes: 0
Views: 6564
Reputation: 691635
It's extremely simple. At line 58, you're calling a method on jdbcTemplateObject
.
So, where is this variable initialized to something other than null?
@Override
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
Is this method ever called? No. Your main does the following:
InstitutionJdbcTemplate ins = new InstitutionJdbcTemplate();
List<InstitutionJdbcTemplate> inst = (List<InstitutionJdbcTemplate>) ins.getInstitution("_CIT");
So, since setDataSource() is never called on your new InstitutionJdbcTemplate object, the variable is null, and calling a method on null throws an NPE.
Your intention is probably that the setDataSource() method should be called by Spring. That is only possible if Spring instantiates InstitutionJdbcTemplate, and not you. By doing new InstitutionJdbcTemplate()
, you're completely ignoring Spring. All you're doing is creating an object, and Spring has no way of knowing that. So, assuming you have a bean of type InstitutionJdbcTemplate defined in your Spring configuration, what you need to do is get this bean from the Spring context:
InstitutionJdbcTemplate ins = appContext.getBean(InstitutionJdbcTemplate.class);
Read http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/htmlsingle/#beans-factory-client
Upvotes: 1