I've been getting a NullPointerException
around the jdbcTemplate.queryForObject
, when I try the following code. I am trying a simple select statement against a Netezza database. Below is an example of how my code is setup:
class AccessDOA.java:
package WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
public class AccessDAO {
@Autowired
public JdbcTemplate jdbcTemplate;
public Person getUserDetails() {
String query = "Select name, email, age from user LIMIT 1";
return jdbcTemplate.queryForObject(query, (resultSet, i) -> {
return new Person(resultSet.getString(1), resultSet.getString(2), resultSet.getInt(3));
});
}
}
The Controller is calling this like so:
PersonController.java
package WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PersonController {
@RequestMapping(value = "/person")
public
@ResponseBody
Person dailyStats(@RequestParam Integer id) {
System.out.println("got this far");
AccessDAO newConn = new AccessDAO();
Person newPerson = new Person();
newPerson = newConn.getUserDetails();
return newPerson;
}
}
I initially had the code to call my database inside the PersonController
which worked perfectly and returned the person object to postman. I however want to keep my DB calls in their own class so I've been moving that logic to the AccessDAO.java
.