identify Nullpointerexception

Asked

Viewed 81 times

1

I am JSP, and during the test of a DAO, I came up with a Nullpointerexception here. However, I am not able to identify why.

I am using Preparedstatement and pulling the count "INSERT", which I created, into a method. Follow the code:

private static final String INSERIR ="INSERT INTO produto (ds_tipo_produto, nm_produto, nr_quantidade,\"\r\n vl_preco, ds_tipo_negociacao) VALUES (? , ? , ? , ?, ?)";
private static final String ALTERAR ="UPDATE produto SET ds_tipo_produto =?, nm_produto =?, nr_quantidade=?,vl_preco=?, ds_tipo_negociacao=?) ";
private static final String EXCLUIR = "DELETE FROM produto WHERE id_produto = ?";
private static final String LISTAR_POR_CÓDIGO ="SELECT * FROM produto WHERE id_produto=?";
private static final String LISTAR_TUDO ="SELECT * FROM produto";

private static Connection conn ;
public produtoDAO() {   
    conn = ConexaoBD.getConnection();
}

public static void registrarItem(Produto p) {
    try {
        PreparedStatement ps = conn.prepareStatement(INSERIR);
        ps.setString(1, p.getTipoProduto());
        ps.setString(2, p.getNomeProduto());
        ps.setInt(3, p.getQtdProduto());
        ps.setDouble(4, p.getPreco());
        ps.setString(5, p.getTipoNegociacao());
        ps.executeUpdate();
        ps.close(); 
    }

and here’s my test class:

public class teste {
    public static void main(String[] args) {
        Produto produto = new Produto();
        produto.setTipoProduto("tipo1");
        produto.setNomeProduto("item1");;
        produto.setQtdProduto(5);;
        produto.setPreco(10.00);;
        produto.setTipoNegociacao("Venda");
        produtoDAO.registrarItem(produto);

        //System.out.println(produto);
    }
}

What do you suggest?

  • The beginning there went bad, but I hope you understand. rsrsrs

  • It tries to identify the exact error, makes a Try catch in this method, and if it gives Nullpointerexception, plays on the console the error, making ex.getMessage();

  • So my catch is like this: catch(SQLException ex) {
 
 ex.printStackTrace();
 
 } if I exchange Sqlexception for null Pointer, it will give error in variables.

  • When null Pointer enters Sqlexception ?

  • The test class calls the method, hence the method already hangs at the time it assigns preparedstatement

  • My answer didn’t help?

  • 1

    Sorry, Igor! I’ll check now, I was busy, thank you for the explanation. In fact, I had forgotten about the class naming convention. already I say if it worked.

Show 2 more comments

1 answer

0

Something tells me the DAO class is called produtoDAO.

Starting from this premise, in the method main() you do: produtoDAO.registrarItem(produto);. By doing this the static method registrarItem() is called.

Note that no instance has been created. Thus the constructor is not called. So the line that initializes the attribute conn is not executed:

public produtoDAO() {   
    conn = ConexaoBD.getConnection();
}

So when the method registrarItem() is called the attribute conn will be null, resulting in a NullPointerException on the line: PreparedStatement ps = conn.prepareStatement(INSERIR);


To solve: make an instance of the class produtoDAO and use it. If you have no explicit reason for the method registrarItem() be static, leave it as instance (not aesthetic).

Ah, another thing: usually Java classes start with the uppercase letter. That’s a convention. Don’t do produtoDAO. Do ProdutoDAO. It even helps people understand your code to help you. I believe some people thought that produtoDAO was a variable that pointed to an instance of produtoDAO precisely because this is a common practice: ProdutoDAO produtoDAO = new ProdutoDAO();

Browser other questions tagged

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