0
The code below results in error Castexception. How can I fix this?
private void popularTabela(){
ClienteDAO clidao = new ClienteDAO();
Cliente clienteFiltro = new Cliente();
Integer codigoPesq = null;
Cidade cidadeFiltro = ((Cidade) cmbCidPesq.getSelectedItem());
if (!txtCodigoPesq.getText().equals("")){
codigoPesq = Integer.parseInt(txtCodigoPesq.getText());
}
clienteFiltro.setCodigo(codigoPesq);
clienteFiltro.setNome(txtNomePesq.getText());
clienteFiltro.setCidade(cidadeFiltro);
//clienteFiltro.setCidade((Cidade) cmbCidPesq.getSelectedItem());
List<Cliente> clientes = clidao.pesquisar(clienteFiltro);
DefaultTableModel modeloTable = (DefaultTableModel) jTable1.getModel();
while (modeloTable.getRowCount() > 0){
modeloTable.removeRow(0);
}
for (Cliente c : clientes){
modeloTable.addRow(new Object[]{c.getCodigo(),c.getNome(),c.getCidade(),c.getVeiculo()});
}
}
Which line gives this exception?
– Jefferson Quesado
My guess: If the error is in this line: City cityFilter = ((City) cmbCidPesq.getSelectedItem(); Then it is pq the method is returning a value or object that cannot be directly converted to City because it is not a subclass of City. In that case you need to take this value and instantiate a new City.
– Antonio Alexandre
Post the code where the combo populates
– igventurelli
Post your combomodel. If you don’t have one, there’s the cast problem.
– user28595
It is good to include the stack trace in these cases
– Grasshopper
Thanks guys, I found out that my popular table method was before the popular city so I was giving the error. I was able to adjust but thanks for the answers there.
– Henrique Rezende Bulla