I'm trying to use jdbcTemplate to execute sql commands. In this case, I'm trying to insert things into a table. I'm getting a null pointer exception in the line : jdbcTemplate.execute(subscriptionStatusInsertQuery);
When I try to run the line:
dbMapper.mapToDatabase(newEntry);
where dbMapper is an instance of DBMapper.
@Repository
public class DBMapper {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void mapToDatabase(Status status) {
String id = status.getId();
String subscriptionStatusInsertQuery = "INSERT INTO t1 (ID) VALUES (id)";
jdbcTemplate.execute(subscriptionStatusInsertQuery);
}
}
I've tried not having a setter for jdbcTemplate. I've tried taking out the @Repository annotation. I've tried having a setter that included datasource. Still getting error.
How should I fix this null pointer exception?
Edit:
My application.properties has the following setting:
spring.datasource.url=jdbc:mysql://localhost:3306/ee_internal
spring.datasource.username=ee
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.password=mypassword
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1
And from my pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>