Spring boot : « this.jdbcTemplate » is null
Solved
Rune188
Posted messages
81
Status
Member
-
Rune188 Posted messages 81 Status Member -
Rune188 Posted messages 81 Status Member -
Hello, I’m trying to create a web application that uses:
- SpringBoot
- Mysql
- JDBC
- Design pattern: MVC, DAO
- Thymeleaf
- And IntelliJ
I’m simply trying to display the contents of a table from my DB but I always get the following error message (I have checked that the connection to the database in "application.properties" and the problem does not seem to come from the DB connection):
"java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.query(String, org.springframework.jdbc.core.RowMapper)" because "this.jdbcTemplate" is null"
Controller:
@Controller public class LoginController { @GetMapping("/") public String firstPage(){ return "loginPage"; } @GetMapping("/authentification") public String auth(Model model){ DiabeticDAO diaDao = new DiabeticDAO(); List<Diabetic> listDia = diaDao.getAllDiab(); model.addAttribute("listdia",listDia); return "afficheTest"; } }
My model:
import java.sql.Date; public class Diabetic { private int id_diabetic; private int id_doctor; private String name; private String firstname; private Date birthdate; private String mail; private String password; private String phone; private String emergencyContact; private String address; public Diabetic(){ } public Diabetic(int id_diabetic, int id_doctor, String name, String firstname, Date birthdate, String mail, String password, String phone, String emergencyContact, String address) { this.id_diabetic = id_diabetic; this.id_doctor = id_doctor; this.name = name; this.firstname = firstname; this.birthdate = birthdate; this.mail = mail; this.password = password; this.phone = phone; this.emergencyContact = emergencyContact; this.address = address; } public int getId_diabetic(){ return id_diabetic; } public void setId_diabetic(int id_diabetic) { this.id_diabetic = id_diabetic; } public int getId_doctor(){ return id_doctor; } public void setId_doctor(int id_doctor) { this.id_doctor = id_doctor; } public String getName(){ return name; } public void setName(String name) { this.name = name; } public String getFirstname(){ return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public Date getBirthdate(){ return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } public String getMail(){ return mail; } public void setMail(String mail) { this.mail = mail; } public String getPassword(){ return password; } public void setPassword(String password) { this.password = password; } public String getPhone(){ return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmergencyContact(){ return emergencyContact; } public void setEmergencyContact(String emergencyContact) { this.emergencyContact = emergencyContact; } public String getAddress(){ return address; } public void setAddress(String address) { this.address = address; } }
DAO:
@Repository public class DiabeticDAO { @Autowired public JdbcTemplate jdbcTemplate; //@Autowired public SimpleJdbcInsert simpleJdbcInsert; @Autowired public void setDataSource(DataSource dataSource){ this.jdbcTemplate = new JdbcTemplate(dataSource); this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource).withTableName("diabetic") .usingGeneratedKeyColumns("id_diabetic"); } public Diabetic getDiabById(int id){ return jdbcTemplate.queryForObject("select * from Diabetic where id_diabetic=?", new DiabeticRowMapper(),id); } public List<Diabetic> getAllDiab(){ return jdbcTemplate.query("select * from Diabetic",new DiabeticRowMapper()); } public boolean loggin(String mail, String mdp) { DiabeticDAO diaDao = new DiabeticDAO(); List<Diabetic> listDia = diaDao.getAllDiab(); for (int i = 0; i < listDia.size(); i++) { if((listDia.get(i).getMail().equals(mail))&&(listDia.get(i).getPassword().equals(mdp))) { return true; } } return false; } private final class DiabeticRowMapper implements RowMapper<Diabetic> { @Override public Diabetic mapRow(ResultSet rs, int rowNum) throws SQLException{ Diabetic diabetic = new Diabetic(); diabetic.setId_diabetic(rs.getInt("id_diabetic")); diabetic.setId_doctor(rs.getInt("id_doctor")); diabetic.setName(rs.getString("name")); diabetic.setFirstname(rs.getString("firstname")); diabetic.setBirthdate(rs.getDate("birthdate")); diabetic.setMail(rs.getString("mail")); diabetic.setPassword(rs.getString("password")); diabetic.setPhone(rs.getString("phone")); diabetic.setEmergencyContact(rs.getString("emergencyContact")); diabetic.setAddress(rs.getString("address")); return diabetic; } } }
1 answer
Hello,
You should not do
DiabeticDAO diaDao = new DiabeticDAO();in LoginController because that will construct a new DiabeticDAO object instead of using the one instantiated by Spring, which can be obtained via a
@Autowired DiabeticDAO diaDaoand which will be properly initialized with a value in each @Autowired, and in particular the JdbcTemplate NB. in DiabeticDAO the setDataSource method has nothing to do there; it is not up to you to create JdbcTemplate objects since they are injected by Spring. -- Trust does not exclude control
Thanks :)