0

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]
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
  • You are facing a ``NullPointerException``. Solve that. – f1sh Feb 20 '17 at 13:35
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – f1sh Feb 20 '17 at 13:35
  • Don't you get a compile error on the line `Customer cus = new Customer();`? Because from what i see, you don't have a default constructor in your Customer class. – drgPP Feb 20 '17 at 13:38
  • Sorry, I edited it...I have the constructor in the class Customer. – user2229430 Feb 20 '17 at 13:41
  • Make sure your `codCli` String is passed to the method properly, use debugger to inspect the problem. – drgPP Feb 20 '17 at 13:46
  • Your sql query is not returning a column with CodiceAzienda so this line wion't work cus.setCodiceAzienda(rs.getString("CodiceAzienda")); – Ravi MCA Feb 20 '17 at 13:55

0 Answers0