readthefuckingmanual.net

[SOLVED] java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(String, Object[])" because "this.jdbcTemplate" is null

Error added: 2023-01-29T21:40:32Z

0 people waiting for the answer...

1 answers found.

Answer 2743 (100.0% helpful)

I had this problem, and couldn't understand why as jdbcTemplate was a final field, and was initialised in the constructor.

Changing my MVC method from private to public stopped this error.

@Controller
@RequestMapping("/path")
public class MyController {

	private final JdbcTemplate jdbcTemplate;

	public MyController(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

@GetMapping
private String get() {
  getEmail(...);
}

	private Optional<String> getEmail(String username) {
		SqlRowSet row = jdbcTemplate.queryForRowSet("SELECT email FROM emails WHERE username = ?", username);
		if (row.next()) {
			return Optional.of(row.getString("email"));
		}
		return Optional.empty();
	}

to

@GetMapping
public String get() {
  getEmail(...);
}

Permalink

Add an answer/solution

If you know the answer, please add your own solution below.
If you don't know, but find out later, please come back and share your answer - there will be other people struggling with this too.


Please enter 61948 here

If you want to be notified via email when this is solved, enter your email address here: