Data insertion error java null, Java Postgre Database

1

I'm trying to insert data into a table in the database, but it returns error "null."

public void cadastrarChamadas() {

    Chamadas1 cha= null;
    String sql = "Insert into tb_chamadas(cha_cod , cha_nome, cha_defeito, cha_datainicio, cha_datafinal, cha_horainicio, cha_horafinal, cha_numerovisitas, cha_tipocontrato, cha_atividadesrealizadas) values(?,?,?,?,?,?,?,?,?,?)";

    //Capturo o valor do campoTextField e coloco em valorTextField.
    try {
        int cont=0;


        pst = con.prepareStatement(sql);

        //seta os valores
        pst.setInt(1,cha.getCod());
        pst.setString(2,cha.getNome() );
        pst.setString(3,cha.getDefeito());
        pst.setString(4,cha.getDataInicio());
        pst.setString(5, cha.getDataFinal());
        pst.setInt(6, cha.getHoraInicio());
        pst.setInt(7, cha.getHoraFinal());
        pst.setInt(8, cont+1);
        pst.setString(9, cha.getTipoServico());
        pst.setString(10, cha.getAtividadesrealiadas());



        //pst.setInt(14,Integer.parseInt(txtId.getText()));
        pst.execute();
        JOptionPane.showMessageDialog(null, "Dados das Chamadas Salvas:");

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Erro na Inserção de dados:\n Erro:" + e.getMessage());
    }

}

Now my database.

    
asked by anonymous 20.01.2017 / 12:33

1 answer

1

The cha object must be instantiated, received by parameter, or be an attribute of the class that contains the cadastrarChamadas() method.

It should look something like this:

public void cadastrarChamadas(Chamada1 cha) {

and then use the cha passed as parameter.

The line that is casting NullPointerException is probably the

pst.setInt(1,cha.getCod());

Because in your code the cha is null, according to the Chamadas1 cha = null line.

    
20.01.2017 / 13:47