1

I try to make Test code about JTable input and refresh.

Insert and delete is working well, but if I delete or insert data after sort the table, it make's exception:

"AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException : 9>=0" ..

Here's my test code.

How do I fix it?

and.. any other advice?

public class Test extends JFrame{

private DefaultTableModel modelTest = new DefaultTableModel();
private JTable tableTest = new JTable(modelTest);
private JScrollPane paneTest = new JScrollPane(tableTest);

private JButton button1 = new JButton("pattern1");
private JButton button2 = new JButton("pattern2");
private JButton button3 = new JButton("delete");

private void compInit(){
    paneTest.setBounds(0, 0,778, 300);
    button1.setBounds(250, 320,80,20);
    button2.setBounds(450,320,80,20);
    button3.setBounds(300,400,80,20);

    DefaultTableModel tmp = modelTest;

    tmp.addColumn("  ");
    tmp.addColumn("col1");
    tmp.addColumn("col2");
    tmp.addColumn("col3");
    tmp.addColumn("col4");
    tmp.addColumn("col5");
    tmp.addColumn("col6");
    tmp.addColumn("col7");

    try {
        tableTest.setDefaultRenderer(Class.forName("java.lang.String"), new DefaultTableCellRenderer());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    tableTest.setAutoCreateRowSorter(true);
    tableTest.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    tableTest.getColumnModel().getColumn(0).setPreferredWidth(20);
    tableTest.getColumnModel().getColumn(1).setPreferredWidth(45);
    tableTest.getColumnModel().getColumn(2).setPreferredWidth(110);
    tableTest.getColumnModel().getColumn(3).setPreferredWidth(60);
    tableTest.getColumnModel().getColumn(4).setPreferredWidth(100);
    tableTest.getColumnModel().getColumn(5).setPreferredWidth(227);
    tableTest.getColumnModel().getColumn(6).setPreferredWidth(100);
    tableTest.getColumnModel().getColumn(7).setPreferredWidth(100);
    tableTest.getTableHeader().setForeground(new Color(105,105,105));

    this.add(button1);
    this.add(button2);
    this.add(button3);
    this.add(paneTest);
}

private void pattern1(){
    for(int i = 0;i<10;i++){
        Vector rowData = new Vector<>();
        rowData.add(false);
        rowData.add(i+1);
        rowData.add("a : " + i);
        rowData.add("b : " + i);
        rowData.add("c : " + i);
        rowData.add("d : " + i); 
        rowData.add("e : " + i); 
        rowData.add("f : " + i); 
        modelTest.addRow(rowData);
    }
}

private void pattern2(){
    for(int i = 0;i<10;i++){
        Vector rowData = new Vector<>();
        rowData.add(false);
        rowData.add(i+1);
        rowData.add("z : " + i); 
        rowData.add("y : " + i);
        rowData.add("x : " + i); 
        rowData.add("w : " + i); 
        rowData.add("v : " + i); 
        rowData.add("u : " + i); 
        modelTest.addRow(rowData);
    }
}

private void delete(){
    DefaultTableModel tmp = modelTest;
    tmp.getDataVector().removeAllElements();
    tableTest.repaint();
}


private void eventInit(){
    button1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            pattern1();
        }
    });
    button2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            pattern2();
        }
    });
    button3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            delete();
        }
    });
}

public Test(){
    this.setLayout(null);
    this.compInit();
    this.eventInit();
    this.setSize(778, 500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
}

public static void main(String[] ar){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new Test();
        }
    }); 
}

}


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at javax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getValueAt(Unknown Source)
at javax.swing.table.TableRowSorter$TableRowSorterModelWrapper.getStringValueAt(Unknown Source)
at javax.swing.DefaultRowSorter.compare(Unknown Source)
at javax.swing.DefaultRowSorter.access$100(Unknown Source)
at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
at javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
at java.util.Arrays.binarySearch0(Unknown Source)
at java.util.Arrays.binarySearch(Unknown Source)
at javax.swing.DefaultRowSorter.insertInOrder(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted0(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
at javax.swing.JTable.notifySorter(Unknown Source)
at javax.swing.JTable.sortedTableChanged(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at timer.Test.pattern1(Test.java:77)
at timer.Test.access$0(Test.java:66)
at timer.Test$1.actionPerformed(Test.java:106)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.DefaultRowSorter.getViewToModelAsInts(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted0(Unknown Source)
at javax.swing.DefaultRowSorter.rowsInserted(Unknown Source)
at javax.swing.JTable.notifySorter(Unknown Source)
at javax.swing.JTable.sortedTableChanged(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at timer.Test.pattern2(Test.java:92)
at timer.Test.access$1(Test.java:81)
at timer.Test$2.actionPerformed(Test.java:111)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3329872
  • 111
  • 1
  • 9

2 Answers2

1

Whenever you change your model vector directly, make sure to notify the container about this. You fail to do that in the following method:

private void delete(){
    DefaultTableModel tmp = modelTest;
    tmp.getDataVector().removeAllElements();
    tableTest.repaint();
}

After the removeAllElements() you should call tmp.fireTableDataChanged(). Something like:

private void delete(){
    DefaultTableModel tmp = modelTest;
    tmp.getDataVector().removeAllElements();
    tmp.fireTableDataChanged();
    tableTest.repaint();
}

Reason: Changes directly to the Model's underlying data vector are not automatically propagated to the View. You are changing a Vector this way, not a Model. Changes to the Model are propagated to the View. Your calls to Model.addRow() inform the View that a row was added. Your Vector.removeAllElements() call is not informed to the View so after that call View and Model are out of sync (that is, if they weren't both empty before). The call to Model.fireTableDataChanged informs the View that the whole table data in the Model has changed. After this call they are in sync again. When Model and View are out of sync, you can expect ArrayOutOfBoundException's to occur e.g. during sorting.

TT.
  • 15,774
  • 6
  • 47
  • 88
0

I also had this problem recently - a NullPointerException caused by DefaultRowSorter.getViewToModelAsInts.

The problem for me is that I was trying to directly update the GUI when an exception was thrown in SwingWorker's doInBackground() method. I found the exact solution to my particular problem here. Maybe that will help you or at least point you in the right direction as I wouldn't be surprised if your problem was because you are trying to manipulate the GUI from the wrong thread.

Community
  • 1
  • 1
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253