I am a beginner to Spring Boot/MVC. I have been trying to build a very basic example of querying a table containing master data for doctors.However I am repeatedly getting "java.lang.NullPointerException".Most probably because jdbcTemplate object is not getting initialized. Some of the other users also faced this issue however in their case the problem was resolved after either including starter-jdbc jar or after removing usage of new operator to create jdbctemplate object.I have already factored these suggestions in my code. Any help on the matter would be appreciated.My code snippets are as following:
1. application.properties :
server.port=8181
spring.datasource.url = jdbc:mysql://localhost:3306/my_sample_schema
spring.datasource.username = qwerty
spring.datasource.password = qwerty
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driverClassName=com.mysql.jdbc.Driver
debug=true
2. Test123Application.java
package com.example;
....all imports...
@SpringBootApplication
public class Test123Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Test123Application.class, args);
}
}
3.Testcontroller.java
package com.example.controller;
....all imports...
@Controller
public class TestController {
@RequestMapping(value = "/")
public String demofunction(){
return "dummytemplate";
}
@RequestMapping("/default")
public String demofunction2(Model model){
Docrepo docrepo = new Docrepoimpl();
List<Docmaster> listContact = docrepo.list();
model.addAttribute("listContact", listContact);
return "dummytemplate2";
}
}
4. Docrepoimpl.java
package com.example.repository;
----all imports---
@Configuration
@Repository
public class Docrepoimpl implements Docrepo{
@Autowired
private JdbcTemplate jdbcTemplate;
public void adddoctor(Docmaster doc){
String sql = "INSERT INTO docmastertable (docid,name,yoe,speciality,degree,college,hospital,regno)"
+ " VALUES (?,?,?,?,?,?,?,?)";
jdbcTemplate.update(sql, doc.getdocid(), doc.getname(),doc.getyoe(), doc.getspeciality(),doc.getdegree(),doc.getcollege(),doc.gethospital(),doc.getregno());
}
public List <Docmaster> list(){
String sql = "SELECT * FROM docmastertable";
if(jdbcTemplate != null)
System.out.println("jdbc seems ok...");
else
System.out.println("jdbc is null...");
List<Docmaster> listContact = jdbcTemplate.query(sql, new RowMapper<Docmaster>() {
@Override
public Docmaster mapRow(ResultSet rs, int rowNum) throws SQLException {
Docmaster doc = new Docmaster();
doc.setdocid(rs.getString("docid"));
doc.setname(rs.getString("name"));
doc.setyoe(rs.getInt("yoe"));
doc.setspeciality(rs.getString("speciality"));
doc.setdegree(rs.getString("degree"));
doc.setcollege(rs.getString("college"));
doc.sethospital(rs.getString("hospital"));
doc.setregno(rs.getString("regno"));
return doc;
}
});
return listContact;
}
}
Error Dump:
java.lang.NullPointerException: null at com.example.repository.Docrepoimpl.list(Docrepoimpl.java:52) ~[classes/:na] at com.example.controller.TestController.demofunction2(TestController.java:43) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_40] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_40] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_40] ...
Actually you are creating the repo directly in your controller class with a new statement. I'd recommend to also inject your Repo into your controller class by creating a local member and annotate it with @Autowired. If you don't want to follow this way you can also ask the Context to return a ready to use bean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With