The below code I had supposed to return customers that is equal to the String of codCli
.
I've a database of mysql
. When I try to query all it works fine but specific select is not working. In fact I want to put the queried result in a bean/container like Customer or in a list for manipulation.
private JdbcOperations jdbc;
.....
public Customer getUser(String codCli) {
try {
Customer customer = jdbc.queryForObject(
"SELECT id, codice_cliente, Indrizzo FROM customers WHERE codice_cliente = ?",
new Object[] { codCli },
new RowMapper<Customer>() {
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
Customer cus = new Customer();
cus.setId(rs.getLong("id"));
cus.setCodiceCliente(rs.getString("codice_cliente"));
// customer.setAge(rs.getInt("age"));
cus.setCodiceAzienda(rs.getString("CodiceAzienda"));
return cus;
}
}
);
return customer;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
Here is my customer class:
package com.example;
public class Customer {
private Long id;
private String firstName, lastName, CodiceCliente, Indrizzo, CodiceAzienda;
public Customer() {
// TODO Auto-generated constructor stub
}
public Customer(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(String CodiceCliente, String codiceAz, String Indrizzo) {
this.id = id;
this.CodiceAzienda = codiceAz;
this.lastName = lastName;
this.CodiceCliente = CodiceCliente;
this.Indrizzo = Indrizzo;
}
public void setId(Long id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setCodiceCliente(String lastName) {
this.CodiceCliente = lastName;
}
public void setCodiceAzienda(String lastName) {
this.CodiceAzienda = lastName;
}
public void setIndrizzo(String firstName) {
this.Indrizzo = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getId() {
return this.id;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
public String getCodiceCliente() {
return this.CodiceCliente;
}
public String getCodiceAzienda() {
return this.CodiceAzienda;
}
public String Indrizzo() {
return this.Indrizzo;
}
}
Here is the error message:
java.lang.NullPointerException: null
at com.example.CustomerService.getUser(CustomerService.java:87) ~[classes/:na]