Why are you giving castException error?

Asked

Viewed 40 times

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?

  • 1

    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.

  • 1

    Post the code where the combo populates

  • 1

    Post your combomodel. If you don’t have one, there’s the cast problem.

  • 1

    It is good to include the stack trace in these cases

  • 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.

Show 1 more comment
No answers

Browser other questions tagged

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