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();
}
}
}
Possible duplicate of What is Nullpointerexception and what are its main causes?
– Icaro Martins
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
– Icaro Martins
Read this: https://answall.com/q/172909/132
– Victor Stafusa