I'm trying to autowire jdbctemplate in MessageBoxDAO class (which i guess is working fine), then i create autowired DAO object in the controller to get the latest ID in order to counter not getting repetitive IDs and start where it ended.
Here's the code:
@Component
public class MessageBoxDAO {
@Autowired
JdbcTemplate jdbcTemplate;
...
public long getLatestID() {
return jdbcTemplate.queryForObject("SELECT id FROM messages ORDER BY id DESC LIMIT 1", Integer.class);
}
}
@RestController
public class MessageBoxController {
@Autowired
MessageBoxDAO dao;
private final AtomicLong counter = new AtomicLong(dao.getLatestID());
...
}
For some unknown to me reason "dao" is null, hence counter cannot be constructed and the program stops
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.CRUD.MessageBoxController]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.example.CRUD.MessageBoxDAO.getLatestID()" because "this.dao" is null
I'm new to spring boot and I understand it might be a very simple problem, but can somebody point out where am I being wrong?