Quando executo o metodo getByFilter() ele me retorna com erro de NullPointerException. Não vejo nada de errado na classe.
public class PessoaDAO extends DALTemplate<Pessoa> {
public PessoaDAO() {
super();
}
protected String getSelectQuery(){
return "Select * From Pessoa";
}
protected String getDeleteQuery(){
return "Delete From Pessoa Where id = ?";
}
protected String getInsertQuery(){
return "Insert Into Pessoa (nome, idade) Values (?,?) ";
}
protected String getUpdateQuery(){
return "Update Pessoa Set nome = ?, idade = ? Where id = ?";
}
protected boolean insert(Pessoa value) throws Exception
{
try
{
this.setCommand(this.getConnection().prepareStatement(this.getInsertQuery()));
this.setParameters(value);
if(this.getCommand().executeUpdate() > 0)
return true;
}
catch(Exception ex)
{
throw new Exception("Erro ao inserir Pessoa.");
}
finally
{
this.getCommand().close();
this.getConnection().close();
}
return false;
}
protected boolean update(Pessoa value) throws Exception
{
try
{
this.setCommand(this.getConnection().prepareStatement(getUpdateQuery()));
this.setParameters(value);
if(this.getCommand().executeUpdate() > 0)
return true;
}
catch(Exception ex)
{
throw new Exception("Erro ao atualizar Pessoa.");
}
finally
{
this.getCommand().close();
this.getConnection().close();
}
return false;
}
public boolean delete(Pessoa value) throws Exception {
try
{
this.setCommand(this.getConnection().prepareStatement(getDeleteQuery()));
this.getCommand().setInt(1, value.getId());
if(this.getCommand().executeUpdate() > 0)
return true;
}
catch(Exception ex)
{
throw new Exception("Erro ao deletar Pessoa.");
}
finally
{
this.getCommand().close();
this.getConnection().close();
}
return false;
}
public Collection<Pessoa> getByIdade(int idade) throws Exception{
try
{
String query = this.getSelectQuery() + " Where idade = ?";
this.setCommand(this.getConnection().prepareStatement(query));
this.getCommand().setInt(1, idade);
return super.convertToCollection(this.getCommand().executeQuery());
}
catch(Exception ex)
{
throw new Exception("Erro ao obter Pessoas do sistema.");
}
finally
{
this.getCommand().close();
this.getConnection().close();
}
}
public Pessoa getByFilter(int id) throws Exception{
ResultSet resultSet = null;
try
{
String query = this.getSelectQuery() + " Where id = ?";
this.setCommand(this.getConnection().prepareStatement(query));
this.getCommand().setInt(1, id);
resultSet = this.getCommand().executeQuery();
if(resultSet.next()){
return this.convert(resultSet);
}
return null;
}
catch(Exception ex)
{
throw new Exception("Erro ao obter Pessoa por id.");
}
finally
{
resultSet.close();
this.getCommand().close();
this.getConnection().close();
}
}
protected Pessoa convert(ResultSet resultSet) throws Exception
{
try
{
Pessoa pessoa = new Pessoa();
pessoa.setId(resultSet.getInt("id"));
pessoa.setNome(resultSet.getString("nome"));
pessoa.setIdade(resultSet.getInt("idade"));
return pessoa;
}
catch(Exception ex)
{
throw new Exception("Erro ao preencher Pessoa.");
}
}
protected void setParameters(Pessoa value) throws Exception
{
try
{
this.getCommand().setString(1, value.getNome());
this.getCommand().setInt(2, value.getIdade());
}
catch(Exception ex)
{
throw new Exception("Erro ao preencher parametros.");
}
}
}