Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I used filter on my Jtable and I get text to filter from Jtextfield. But sometime, when I enter text in Jtextfield, this exception will display:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 4
	at java.desktop/javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:508)
	at java.desktop/javax.swing.JTable.convertRowIndexToModel(JTable.java:2686)
	at View$4.valueChanged(View.java:137)
	at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:224)
	at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:204)
	at java.desktop/javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:251)
	at java.desktop/javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:448)
	at java.desktop/javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:458)
	at java.desktop/javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:619)
	at java.desktop/javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:463)
	at java.desktop/javax.swing.JTable$SortManager.restoreSelection(JTable.java:4091)
	at java.desktop/javax.swing.JTable$SortManager.processChange(JTable.java:4059)
	at java.desktop/javax.swing.JTable.sortedTableChanged(JTable.java:4191)
	at java.desktop/javax.swing.JTable.sorterChanged(JTable.java:3881)
	at java.desktop/javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:341)
	at java.desktop/javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:332)
	at java.desktop/javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:621)
	at java.desktop/javax.swing.DefaultRowSorter.setRowFilter(DefaultRowSorter.java:420)
	at View.Filter(View.java:250)
	at View$1.insertUpdate(View.java:72)
	at java.desktop/javax.swing.text.AbstractDocument.fireInsertUpdate(AbstractDocument.java:226)
	at java.desktop/javax.swing.text.AbstractDocument.handleInsertString(AbstractDocument.java:780)
	at java.desktop/javax.swing.text.AbstractDocument.insertString(AbstractDocument.java:739)
	at java.desktop/javax.swing.text.PlainDocument.insertString(PlainDocument.java:131)
	at java.desktop/javax.swing.text.AbstractDocument.replace(AbstractDocument.java:698)
	at java.desktop/javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1339)
	at java.desktop/javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884)
	at java.desktop/javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1810)
	at java.desktop/javax.swing.JComponent.processKeyBinding(JComponent.java:2947)
	at java.desktop/javax.swing.JComponent.processKeyBindings(JComponent.java:2995)
	at java.desktop/javax.swing.JComponent.processKeyEvent(JComponent.java:2909)
	at java.desktop/java.awt.Component.processEvent(Component.java:6403)
	at java.desktop/java.awt.Container.processEvent(Container.java:2266)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1952)
	at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:883)
	at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1150)
	at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1020)
	at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:848)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4882)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
	at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


What I have tried:

I don't know why. Please explain this for me and how can I fix it.
Posted
Updated 20-Jan-22 9:35am
Comments
Richard MacCutchan 19-Jan-22 11:06am    
The message is quite clear. You are using the value 9 to access something in an array which only contains 4 elements. However we have no way of telling why that happens. You will need to use your debugger to capture more information.
Member 15493982 19-Jan-22 21:02pm    
No no,some time it can be value 0 out of bounds for length 0.
It like : Index X out of bounds for length Y (X,Y is a number)
And I don't set length for text field
Richard MacCutchan 20-Jan-22 3:19am    
Well you still have to use your debugger to find out where it is happening and why. It is impossible for anyone here to guess what your code does.
Member 15493982 20-Jan-22 4:39am    
I see two:
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
selectedrow = table.convertRowIndexToModel(viewrow);
}
}
);

sorter.setRowFilter(rf);



Richard MacCutchan 20-Jan-22 5:01am    
And as I keep suggesting, you can only find the answer by using the debugger and finding out where, and why, these invalid indexes are being generated.

1 solution

Check this thread, you could find suggested fix.

JDK-8032874 : ArrayIndexOutOfBoundsException in JTable while clearing data in JTable[^]

Basically you need to add if condition for convertRowIndexToModel line where you're getting error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900