0

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.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
  • And where exactly do you get the NPE? – dunni Apr 26 '16 at 19:59
  • NPE occurs with the line: return jdbcTemplate.queryForObject(query, (resultSet, i) -> { – Criostoir Apr 26 '16 at 20:02
  • 1
    Put simply, when you're using Dependency Injection, let spring manage your bean life cycle. You're new-ing `AccessDAO` in the service and also used `Autowired` in dao. Since you're manually instantiating the dao class, that `Autowired` dependecy would be null and referencing it would cause a `NullPointerException` – Ali Dehghani Apr 26 '16 at 20:03
  • So how should I call the method in AccessDAO? – Criostoir Apr 26 '16 at 20:07
  • `Autowire` `AccessDAO` to `PersonController` and use that autowired dependency. Also, remove that `AccessDAO newConn = new AccessDAO();` – Ali Dehghani Apr 26 '16 at 20:08
  • If I try: @Autowired AccessDAO newConn; I am getting "No qualifying bean of type [WebService.AccessDao] found for dependency – Criostoir Apr 26 '16 at 20:15
  • That's because your `AccessDAO` is not a Spring bean; add a `@Component` annotation to it and make sure Spring can find it. – Jesper Apr 27 '16 at 06:38

0 Answers0