Null Pointer Exception when loading a JTextField

0

I have my Auto class

public class Auto {

    int id_auto;
    String modelo;
    String color;
    int anio;
    String marca;

    public int getId_auto() {
        return id_auto;
    }

    public String getModelo() {
        return modelo;
    }

    public String getColor() {
        return color;
    }

    public int getAnio() {
        return anio;
    }

    public String getMarca() {
        return marca;
    }

    public void setId_auto(int id_auto) {
        this.id_auto = id_auto;
    }

    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setAnio(int anio) {
        this.anio = anio;
    }

    public void setMarca(String marca) {
        this.marca = marca;
    }

    public Auto(int id_auto, String modelo, String color, int anio, String marca) {
        this.id_auto = id_auto;
        this.modelo = modelo;
        this.color = color;
        this.anio = anio;
        this.marca = marca;
    }



}

A method in my class Manager to modify the Auto that the user selects from a JTable

public Auto modificarAuto(int id)
    {
        Auto a = null;
        String sql = "SELECT a.id_auto, a.modelo, a.color, a.anio, m.marca FROM auto a, marca m WHERE a.id_marca = m.id_marca AND id_auto = ?";

        try(Connection cn = DriverManager.getConnection(url, user, password);
                PreparedStatement ps = cn.prepareStatement(sql);)
        {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();

            while (rs.next())
            {
                int id_auto = rs.getInt("id_auto");
                String modelo = rs.getString("modelo");
                String color = rs.getString("color");
                int anio = rs.getInt("anio");
                String marca = rs.getString("marca");

                a = new Auto(id_auto,modelo,color,anio,marca);
            }
        }
        catch(SQLException e)
        {
            JOptionPane.showMessageDialog(null, e, "ERROR", JOptionPane.ERROR_MESSAGE);
        }


        return a;
    }

And the window that will open with the JTextFields loaded with the corresponding data.

ModificarAuto(int id)
{
    this.id = id;
    Auto a = ge.modificarAuto(this.id);
    txtId.setText(String.valueOf(a.getId_auto()));
}

The problem I have is that when I use the .setText method I get the NullPointerException error. I do not have problems to show the result of a.getId_auto by console, but if when saving it in a JTextField

    
asked by Lucas. D 20.09.2017 в 01:20
source

1 answer

0

Solved by adding this (); to the beginning of my method

ModificarAuto(int id)
    {
        this();
        id_a = id;
        Auto a = ge.modificarAuto(id_a);
        txtId.setText(String.valueOf(a.getId_auto()));
        txtModelo.setText(a.getModelo());
        txtColor.setText(a.getColor());
        txtAnio.setText(String.valueOf(a.getAnio()));
        cboMarca.setSelectedItem(a.getMarca());
    }
    
answered by 20.09.2017 в 02:17