Exception ArrayIndexOutOfBoundsException:0> = 0,同时从JTable检索值

问题描述 投票:0回答:1

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

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

我在做什么错?

java jtable
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.