Erro java.lang.IndexOutOfBoundsException[Resolvido]

4 respostas
look_evil

Salve pessoal,
Bom... sou iniciante em java, e me deparei o seguinte erro:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 4, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at br.com.agenda.Principal.tbContatosLinhaSelecionada(Principal.java:37)
at br.com.agenda.Principal.access$200(Principal.java:21)
at br.com.agenda.Principal$2.valueChanged(Principal.java:108)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
at javax.swing.JTable.tableRowsDeleted(JTable.java:4512)
at javax.swing.JTable.tableChanged(JTable.java:4415)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:464)
at br.com.agenda.Principal.mostrarContatos(Principal.java:457)
at br.com.agenda.Principal.listarContatos(Principal.java:480)
at br.com.agenda.Principal.btPesquisarActionPerformed(Principal.java:484)
at br.com.agenda.Principal.access$000(Principal.java:21)
at br.com.agenda.Principal$1.actionPerformed(Principal.java:97)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Alguem pode me ajudar?
desde já agraço.

O codigo:
private void tbContatosLinhaSelecionada(JTable tb){
        if(tb.getSelectedRow() != -1){
            tfNome.setText(contatos.get(tb.getSelectedRow()).getNome());//linha que aponta erro
            tfEmail.setText(contatos.get(tb.getSelectedRow()).getEmail());
            tfTelefone.setText(contatos.get(tb.getSelectedRow()).getTelefone());
            tfCidade.setText(contatos.get(tb.getSelectedRow()).getCidade());
            tfCelular.setText(contatos.get(tb.getSelectedRow()).getCelular());
            tfEndereco.setText(contatos.get(tb.getSelectedRow()).getEndereco());
            tfBairro.setText(contatos.get(tb.getSelectedRow()).getBairro());
            tfCep.setText(contatos.get(tb.getSelectedRow()).getCep());
            btExcluir.setEnabled(true);
            btAlterar.setEnabled(true);
            
        }else{
             btAlterar.setEnabled(false);
        }
    }

4 Respostas

S

Mas o que você está tentando fazer ai?

Você está tentando pegar os dados do array contatos pela linha que está selecionada na tabela?

Você tem certeza que os dados que estão na jTable também estão na ArrayList?

OBS: Esse erro significa que a posição que você está tentando pegar no ArrayList não existe!

S

A IndexOutOfBoundsException é uma exception unchecked que é lançada ao tentar acessar uma posição inexistente de algum Array, List, etc. Tente imprimir pelo System.out.println(int x) o tamanho do seu vetor (como o meu conhecemento sobre Swing é proximo de 0 não conheço o metodo que retorna o valor do tamanho do seu JTable, caso não saiba veja na documentação da mesma). Se imprimir 0 (o que provavelmente é o caso), o seu erro estara no momento que você está tentando popular o seu JTable.

lina

Oi,

No caso dele, a própria exceção já disse o tamanho do Array:

java.lang.IndexOutOfBoundsException: Index: 4, Size: 1

Ou seja, está tentando pegar o índice 4 de um Array com apenas 1 posição. Verifique a sua lógica, pois o Array “contatos” não tem tamanho 4 e sim 1.

Tchauzin!

look_evil

Vlw man… obrigado pela ajuda.

Criado 23 de julho de 2012
Ultima resposta 24 de jul. de 2012
Respostas 4
Participantes 4