I am starting in java and am having the error java.lang.Nullpointerexception

Asked

Viewed 69 times

1

ERROR:

Exception in thread "main" java.lang.NullPointerException
    at br.com.drogaria.dao.FabricanteDAO.excluir(FabricanteDAO.java:33)
    at br.com.drogaria.dao.FabricanteDAO.main(FabricanteDAO.java:64)

Although this error appears it is deleting the data in the BD. I am using the eclipse ide

CODE:

package br.com.drogaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.com.drogaria.domanin.Fabricante;
import br.com.drogaria.factory.ConexaoFactory;

public class FabricanteDAO {
    public void salvar(Fabricante f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO fabricante ");
        sql.append("(descricao) ");
        sql.append("VALUES (?)");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setString(1, f.getDescrição());

        comando.executeUpdate();
    }

    public void excluir(Fabricante f) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("DELETE FROM fabricante ");
        sql.append("WHERE codigo = ? ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());
        comando.setLong(1, f.getCodigo());

        comando.executeUpdate();

    }

    public static void main(String[] args) {

        /*
         * Fabricante f1 = new Fabricante(); f1.setDescrição("DESCRICAO 1");
         * 
         * Fabricante f2 = new Fabricante(); f2.setDescrição("DESCRICAO 2");
         * 
         * FabricanteDAO fdao = new FabricanteDAO();
         * 
         * try { fdao.salvar(f1); fdao.salvar(f2);
         * System.out.println("Os fabricantes foram salvos com sucesso"); } catch
         * (SQLException e) { System.out.println("Erro ao salvar um dos fabricantes");
         * e.printStackTrace(); }
         */

        Fabricante f1 = new Fabricante();
        f1.setCodigo(1L);

        Fabricante f2 = new Fabricante();
        f1.setCodigo(5L);

        FabricanteDAO fdao = new FabricanteDAO();

        try {
            fdao.excluir(f1);
            fdao.excluir(f2);
            System.out.println("Os fabricantes foram removidos com sucesso!");

        } catch (SQLException e) {
            System.out.println("Ocorreu um erro ao cadastrar um dos fabricantes!");
            e.printStackTrace();

        }

    }
}
  • 2

    Hello @initJavaWeb, Welcome to Sopt, it’s only worth you add a comment beside the lines 33 and 64 indicated in the error reported, this will help the community to identify the error, so click [Edit] below your question -- it is also worth you to take a look at our [Tour] =D

  • Read this: https://answall.com/q/172909/132

1 answer

3

at br.com.drogaria.dao.FabricanteDAO.excluir(FabricanteDAO.java:33)

That line is as follows:

    comando.setLong(1, f.getCodigo());

We know that comando is not null and that f is also not null. So the unsub is that the result of f.getCodigo() be it null.

According to the javadoc, the second parameter is of the type long. So if you pass null instead of this parameter, the result will be a NullPointerException of autounboxing. See more about this in the third case of that answer.

Whereas you called the method setCodigo(Long) in its object Fabricante, but still the method getCodigo() returned null, then the origin of its NullPointerException is not in your class FabricanteDAO, and yes in your class Fabricante.

Despite this, his class FabricanteDAO still have a lot of problems. I strongly recommend that you use the Try-with-Resources. Behold in that other question why do it and how to do it.

Ah, finally this from here:

    StringBuilder sql = new StringBuilder();
    sql.append("INSERT INTO fabricante ");
    sql.append("(descricao) ");
    sql.append("VALUES (?)");

It’s unnecessary. You’re always assembling and reassembling the same SQL piece by piece every time the method salvar executes. The best would be to do so:

private static final String INSERT_SQL = "INSERT INTO fabricante (descricao) VALUES (?)";

Because that way, you build SQL only once when the class is initialized and the cost of building it is practically zero, since it will already be available ready and whole within the bytecode. The same can be said of your method SQL excluir.

Browser other questions tagged

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