gpt4 book ai didi

java - 异常 ArrayIndexOutOfBoundsException : 0>=0 while retrieving a value from JTable

转载 作者:行者123 更新时间:2023-12-01 19:44:26 24 4
gpt4 key购买 nike

我写在这里是因为我到处搜索但没有找到任何解决方案来解决我的问题。我的应用程序使用 .txt 文件来存储 DVD 列表的标题、流派、主要人物和状态,并使用 JTable 来显示列表。我想通过选择 JTable 中的行并按“Elimina”按钮从列表中删除 DVD。我在尝试检索所选行的标题时遇到此异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.base/java.util.Vector.elementAt(Vector.java:463)
at java.desktop/javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:660)
at my.dvdmanager.DvdList_frame.btnYesActionPerformed(DvdList_frame.java:335)
at my.dvdmanager.DvdList_frame$1.actionPerformed(DvdList_frame.java:111)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
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)

这是出现问题的代码段:

DefaultTableModel model = (DefaultTableModel) dvdList_table.getModel();
//get selected row index
int selectedRowInd = 0;
try {
//removes selected row
selectedRowInd = dvdList_table.convertRowIndexToModel(dvdList_table.getSelectedRow());
System.out.println("selectedRowInd:" + selectedRowInd);
model.removeRow(selectedRowInd);

} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}

String path = "dvdlist.txt", dvdToErase = (String) dvdList_table.getModel().getValueAt(selectedRowInd, 0); //Exception raises up here

我做错了什么?

最佳答案

我发现了我的错误(感谢@TT),我必须先检索标题,然后删除该行

关于java - 异常 ArrayIndexOutOfBoundsException : 0>=0 while retrieving a value from JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com