Preciso de ajuda numa exceção: IndexOutOfBoundsException

2 respostas
javer

Em várias casos quando eu manipulo um modelo em uma JTable eu recebe esse tipo de exceção.

Nesse caso específico tenho uma JTable com 4 linhas, eu excluo uma linha no banco de dados e faço o novo carregamento dos dados no modelo, porém recebo essa exceção chatinha.

Eu crio o modelo e associa a tabela assim:
// Passo uma lista vazia pois ainda não tenho os dados
ProducaoTableModel model = new ProducaoTableModel(new ArrayList<Producao>(), tabelaProducao);
Lá no AbstractTableModel tenho esse construtor:
public ProducaoTableModel(ArrayList<Producao> lista, JTable table) {
    this.lista = lista == null ? new ArrayList<Producao>() : lista;
    this.table = table;
    setConfig();
}

private void setConfig() {
    table.setModel(this);
    TableColumnModel cm = table.getColumnModel();

    int[] widths = ProducaoTableModel.COLUMN_WIDTH;
    for (int i = 0; i < widths.length; i++) {
        int width = widths[i];
        cm.getColumn(i).setPreferredWidth(width);
    }
    cm.getColumn(16).setMaxWidth(widths[16]);
    cm.getColumn(16).setMinWidth(widths[16]);
    cm.getColumn(0).setMaxWidth(widths[0]);
    cm.getColumn(0).setMinWidth(widths[0]);
    cm.getColumn(7).setMaxWidth(widths[7]);
    cm.getColumn(7).setMinWidth(widths[7]);
    cm.getColumn(8).setMaxWidth(widths[8]);
    cm.getColumn(8).setMinWidth(widths[8]);
    cm.getColumn(9).setMaxWidth(widths[9]);
    cm.getColumn(9).setMinWidth(widths[9]);
    cm.getColumn(10).setMaxWidth(widths[10]);
    cm.getColumn(10).setMinWidth(widths[10]);

    cm.getColumn(11).setCellRenderer(new ProducaoTableRenderer("dd/MM/yyyy HH:mm"));
    cm.getColumn(12).setCellRenderer(new ProducaoTableRenderer("dd/MM/yyyy HH:mm", true));
    cm.getColumn(13).setCellRenderer(new ProducaoTableRenderer("dd/MM/yyyy HH:mm"));
    cm.getColumn(14).setCellRenderer(new ProducaoTableRenderer("dd/MM/yyyy HH:mm"));

    table.setDefaultRenderer(Integer.class, new ProducaoTableRenderer());
    table.setDefaultRenderer(String.class, new ProducaoTableRenderer());
    table.setDefaultRenderer(Servico.class, new ProducaoTableRenderer());
    table.setDefaultRenderer(Maquina.class, new ProducaoTableRenderer());
    table.setDefaultRenderer(Producao.class, new ProducaoTableRenderer());
    table.setDefaultRenderer(Double.class, new ProducaoTableRenderer());
}
Aí tenho esse método que carrega a lista de dados no modelo:
public void setLista(ArrayList<Producao> lista) {
    this.lista = lista == null ? new ArrayList<Producao>() : new ArrayList<Producao>(lista);
    fireTableDataChanged();
    table.repaint();
}
E o erro ocorre aqui:
public Object getValueAt(int rowIndex, int columnIndex) {
    int row = rowIndex;
    Producao item = (Producao) this.lista.get(row); // O erro ocorre aqui
    ArrayList<BaixaProducao> listaBaixas = new OrdemServicoDAO().getBaixasServicoOrdem(item.getOrdemServico().getNumeroOrdem(), item.getServico().getCodigo());
    double totalProduzida = 0;
    double totalPerdida = 0;
    Iterator<BaixaProducao> it = listaBaixas.iterator();
    while (it.hasNext()) {
        BaixaProducao baixa = it.next();
        totalProduzida += baixa.getQuantidadeProduzida();
        totalPerdida += baixa.getQuantidadePerdida();
    }

O erro ocorre quando faço o recarregamento dos dados no modelo.

2 Respostas

O

Mostra a parte do código que chama o getValueAt(int, int).

Meu adivinhômetro diz que a variavel que é passada como primeiro argumento não é atualizada para o novo tamanho da lista.

javer

Aí que está o problema, no código eu não chamo em lugar algum o getValueAt(int,int), eu simplesmente coloco outra lista no modelo.
Nesse instante acabou de ocorrer novamente, segue parte do StackTrace:

java.lang.IndexOutOfBoundsException: Invalid index at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:497) at javax.swing.JTable.convertRowIndexToModel(JTable.java:2564) at javax.swing.JTable.getValueAt(JTable.java:2639)

Criado 12 de fevereiro de 2009
Ultima resposta 13 de fev. de 2009
Respostas 2
Participantes 2