Nullpointerexception when inserting java data

Asked

Viewed 88 times

0

Hi, I’ve researched several forums, youtube and google outside... I need a lot of help because I have little experience but I really want to learn, in many things I can do but when it’s for dates gives a lock...

the mistake is this...

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at dao.OrigemDAO.inserir(OrigemDAO.java:29)
    at telas.TelaCadastroOrigem.bSalvarActionPerformed(TelaCadastroOrigem.java:151)
    at telas.TelaCadastroOrigem.access$000(TelaCadastroOrigem.java:12)
    at telas.TelaCadastroOrigem$1.actionPerformed(TelaCadastroOrigem.java:51)

this is the DAO class

package dao;


import conexao.ConnectionFactory;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import modelo.Origem;

public class OrigemDAO {

    String sql = null;
    Connection con = ConnectionFactory.getConnection();
    Statement declaracao;
    ResultSet resultado;

public void inserir(Origem o) {
        sql = "INSERT INTO origem (codigo_origem, descricao_origem,data_cadastro, registro) VALUES(?,?,?,?)";

        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, o.getCodigoOrigem());
            ps.setString(2, o.getDescricao());
            ps.setDate(3, new java.sql.Date(o.getDataCadastro().getTime()));
            ps.setInt(4, o.getRegistro());
            System.out.println("sql:"+sql);

            ps.executeUpdate();


            JOptionPane.showMessageDialog(null, "Origem inserida com sucesso!");

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao inserir origem!");
            e.printStackTrace();
        }
    }

    public void atualizar(Origem o) {
        sql = "UPDATE origem SET codigo_origem = ?, descricao_origem = ?, data_cadastro = ?, "
                + " registro = ?, saldo=? WHERE id = ?";

        try {
            PreparedStatement ps = con.prepareStatement(sql);

            ps.setInt(1, o.getCodigoOrigem());
            ps.setString(2, o.getDescricao());
            ps.setDate(3, new java.sql.Date(o.getDataCadastro().getTime()));
            ps.setInt(4, o.getRegistro());
            System.out.println(ps.toString());
            ps.executeUpdate();

            JOptionPane.showMessageDialog(null, "Origem atualizada com sucesso!");

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao atualizar origem!");
            e.printStackTrace();
        }
    }

    public void excluir(Origem o) {
        sql = "DELETE FROM origem WHERE id = ?";

        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, o.getId());

            ps.executeUpdate();
            JOptionPane.showMessageDialog(null, "Origem excluída com sucesso!");

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao excluir origem!");
            e.printStackTrace();
        }
    }

    public ArrayList<Origem> listar() {
        ArrayList<Origem> origens = new ArrayList<>();
        sql = "SELECT * FROM origem";

        try {
            Statement st = con.createStatement();
            ResultSet resultado = st.executeQuery(sql);

            while (resultado.next()) {
               int id = resultado.getInt("id");
               int codigo_origem = resultado.getInt("codigo");
               Date data_cadastro = resultado.getDate("data");
               String nome = resultado.getString("descricao");
               double saldo = resultado.getDouble("saldo");

                Origem origem = new Origem(/*id,codigo_origem,nome, data_cadastro, saldo*/);
                origens.add(origem);
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao recuperar lista de origens!");
            e.printStackTrace();
        }

        return origens;
    }


}

1 answer

0


Well, the answer lies in the error itself.

Your object "o" is null. "o. getDataCadastro(). getTime()" You need to check if the "o" object is not null and if there is data in getDataCadastro(), otherwise nullpointer will occur.

It’s just that, pretty simple.

I hope I’ve helped.

  • Thank you very much I will test it right now and already put...

  • helped yes, just do not know how to pass the value to it, is in the class dao same or can be the way I am doing on screen by actionperformed?

Browser other questions tagged

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