Question

I'm making a GUI project, using MVC design pattern. This program contains JTable component, for which I've created a class MyTableModel which extends AbstractTableModel. As you can see below in MyModelClass, I have two methods addColumn & removeColumn , which have normally add/remove columns from table, until, I have come to the point, when I have to add 3 initial columns with 8 combinations into my constructor, and then try add/remove columns. Here you can see screenshot of program.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    at mvc.TTableModel.getValueAt(TTableModel.java:79)
    at mvc.TTableModel.getColumnClass(TTableModel.java:84)
    at javax.swing.JTable.getColumnClass(JTable.java:2697)
    at javax.swing.JTable.getCellRenderer(JTable.java:5682)
    at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2113)
    at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
    at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
    at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JViewport.paint(JViewport.java:731)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91).

Code snippet

public class TTableModel extends AbstractTableModel implements ThreeInputConstants{

    private ArrayList<Object> columnList;
    private List<boolean[]> data;

    public TTableModel() {
        this.columnList = new ArrayList<>();
        this.data = new ArrayList<>();

        // adds p, q, r
        columnList.add(THREE_INPUT_TITLES[0]);
        columnList.add(THREE_INPUT_TITLES[1]);
        columnList.add(THREE_INPUT_TITLES[2]);

        for (int i = 0; i < 8; i++) {
            boolean[] tempArray = new boolean[3];

            tempArray[0] = P_CONST[i];
            tempArray[1] = Q_CONST[i];
            tempArray[2] = R_CONST[i];

            data.add(i, tempArray);
        }

    }

    public void printListTemp() {
        System.out.println("Column list: " + columnList);
        System.out.println("Data: " + data);
    }

    public void addColumn(String header) {
        this.columnList.add(header);
        this.fireTableStructureChanged();
    }

    public void removeColumn(int columnIndex) {
        if (columnIndex >= 0 && columnIndex < getColumnCount()) {
            this.columnList.remove(columnIndex + INPUT_COLUMN_COUNT);
            this.fireTableStructureChanged();
        }
    }

    @Override
    public String getColumnName(int columnIndex) {
        return columnList.get(columnIndex).toString();
    }

    @Override
    public int getColumnCount() {
        return columnList.size();
    }

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

    @Override
    public void setValueAt(Object value, int row, int column) {
        //data[row][column] = value; 
        data.get(row)[column] = (boolean) value;
        this.fireTableCellUpdated(row, column);
    }

    @Override
    public Object getValueAt(int row, int column) {
        return data.get(row)[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

}

public interface ThreeInputConstants {

    public static final Object THREE_INPUT_TITLES[] = {
            "p", "q", "r"
    };
    public static final boolean P_CONST[] = {
            true, true, true, true, 
            false, false, false, false
    };
    public static final boolean Q_CONST[] = {
            true, true, false, false, 
            true, true, false, false, 
    };
    public static final boolean R_CONST[] = {
            true, false, true, false, 
            true, false, true, false, 
    };
}

UPDATE (FIXED):

public void addColumn(String header) {
        this.columnList.add(header);

        for (int i = 0; i < data.size(); i++) {
            List<Boolean> rowList = data.get(i);
            while (rowList.size() < columnList.size()) {
                rowList.add(false);
            }
        }
        this.fireTableStructureChanged();
    }
Was it helpful?

Solution 2

My guess is that the exception happens after you've called addColumn(). This method adds a new value to the columnList. So the number of column goes from 3 to 4. The table then asks the column class for the last column, and getColumnClass() returns the class of the 4th element of the first row. But the data still contains arrays of 3 elements, so the index 3 doesn't exist in the data.

The length of the column list and the length of every array in the data list should always match: addColumn() should replace each array in the data list by another one containing one more element. Or getValueAt() and the other methods should deal with it to return default values.

OTHER TIPS

You are addding 8 entries to the data ArrayList, each entry contains an array of 3 element.

for (int i = 0; i < 8; i++) {
    boolean[] tempArray = new boolean[3];

    tempArray[0] = P_CONST[i];
    tempArray[1] = Q_CONST[i];
    tempArray[2] = R_CONST[i];

    data.add(i, tempArray);
}

But the error message:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at 

indicates you are trying to retrieve the element with index 3 which is the 4th element. Since your array only have 3 elements, it throws an ArrayIndexOutOfBoundsException exception.

Check your client code that calls the getValueAt method and make sure it does not use index more than 2 for the column.

EDIT:

Try Change your getColumnCount and getRowCount to this:

@Override
public int getColumnCount() {
    return data.size();        
}

@Override
public int getRowCount() {
    return columnList.size();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top