Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: Jtable ArrayIndexOutOfBoundsException

I sometimes get this exception. I dont really know where's problem, i use my implementation of abstracttablemodel. I CANT found the excact place where exception occurs, beacause when i use debugger and run step by step exception doesnt occur.

Exception occurred during event dispatching:
    java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
    at javax.swing.plaf.basic.BasicTableUI.paintGrid(BasicTableUI.java:1931)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1767)
    at com.alee.laf.table.WebTableUI.paint(WebTableUI.java:52)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
    at javax.swing.JComponent.paintComponent(JComponent.java:752)
    at javax.swing.JComponent.paint(JComponent.java:1029)
    at javax.swing.JComponent.paintChildren(JComponent.java:862)
    at javax.swing.JComponent.paint(JComponent.java:1038)
    at javax.swing.JViewport.paint(JViewport.java:747)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
    at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1225)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:786)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1636)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
    at java.awt.Dialog$1.run(Dialog.java:1046)
    at java.awt.Dialog$3.run(Dialog.java:1098)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Dialog.show(Dialog.java:1096)
    at java.awt.Component.show(Component.java:1584)
    at java.awt.Component.setVisible(Component.java:1536)
    at java.awt.Window.setVisible(Window.java:842)
    at java.awt.Dialog.setVisible(Dialog.java:986)
    at ui.MainFrame.queryButtonActionPerformed(MainFrame.java:936)
    at ui.MainFrame.access$700(MainFrame.java:89)
    at ui.MainFrame$9.actionPerformed(MainFrame.java:538)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
    at java.awt.Component.processMouseEvent(Component.java:6288)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

my table model:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entity3.Document;
import entity3.Msg;
import entity3.Senderreceiver;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author work
 */
public class IncomingMailTableModel extends AbstractTableModel{
    public String[] getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(String[] columnNames) {
        this.columnNames = columnNames;
    }
    private String[] columnNames = {"Дата",
        "Тема",
        "Відправник",
        "Статус"};

    private List<Document> documents;

    public IncomingMailTableModel(List<Document> documents) {
        this.documents = documents;
    }

    @Override
    public int getRowCount() {
        return documents.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Document getObjectAtRow(int row) {     
        return documents.get(row);  
    }
    @Override
    public Class getColumnClass(int column) {
        switch (column) {
            case 0:
                return String.class;
            case 1:
                return String.class; 
            case 2:
                return String.class;
            case 3:
                return String.class;

        }
        return null;
    }

    @Override
    public String getValueAt(int row, int column) {
        String toReturn = "";
        Document document = documents.get(row);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Msg msg =document.getMsg();
        ArrayList<Senderreceiver> senderReceivers = new ArrayList<Senderreceiver>(msg.getSenderreceivers());
        //ArrayList<Document> docs = new ArrayList<Document>(document.getMsg().getDocuments());
        switch (column) {
            case 0:
                if (document.getMsg().getReceivingDate() != null) 
                    toReturn = format.format(document.getMsg().getReceivingDate());

                break;
            case 1:
                toReturn = document.getTitle();

                break;

            case 2:
                for (Object o : senderReceivers) {
                    Senderreceiver senderReceiver = (Senderreceiver) o;
                    if (senderReceiver.getRole()==0) {
                        toReturn = senderReceiver.getOrganization().getShortName();
                    }
                }
                break;
            case 3:
                if (document.getMsg().getIsRead()!=null)
                if(document.getMsg().getIsRead()) toReturn = "Прочитано";
                else toReturn="Непрочитано";
                break;
        }
        return toReturn;
    }
}
like image 654
bunnyjesse112 Avatar asked Dec 02 '11 11:12

bunnyjesse112


1 Answers

This can occurr if you are modifying the TableModel (or the data structure that the TableModel "sits on") from a thread other than the Event Dispatch thread.

Another common cause of this error is mixing up view and model indices. For example, if you retrieve the selected row from a JTable using getSelectedRow() the value returned is a view-based index. If you wish to index into the corresponding TableModel you need to convert this value using convertRowIndexToModel.

For the latter case you can try and replicate this exception by sorting the table on different columns and then clicking various rows (in particular first and last), or performing whatever action is required in your UI in order to "process" that row.

like image 110
Adamski Avatar answered Oct 07 '22 01:10

Adamski