Java null data insertion error, Java Postgre database

Asked

Viewed 395 times

1

I am trying to insert data into a table in the database, but 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.

inserir a descrição da imagem aqui

  • 1

    Error "null"? What do you mean? Nullpointerexception, right? If so, it’s because of this line: Chamadas1 cha= null;

  • I can not comment yet, so I will write as if it’s an answer, try to print the generated SQL and run it in the database to see if it works. You may have missed the data type only. I stand by.

1 answer

1

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

It must be something like that:

public void cadastrarChamadas(Chamada1 cha) {

and then use the cha passed as parameter.

The line you’re casting NullPointerException probably is the

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

For in his code the cha is null according to the line Chamadas1 cha = null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.