0

每当我连续两次更新(编辑)单行时,我都会收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:730)
at java.util.Vector.elementAt(Vector.java:473)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at bicrijnmondcustomermanagement.DataView.EditActionPerformed(DataView.java:686)
at bicrijnmondcustomermanagement.DataView.access$400(DataView.java:18)
at bicrijnmondcustomermanagement.DataView$5.actionPerformed(DataView.java:366)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)

这是有问题的代码,我试图在网上查看但没有找到解决方案,所以我转向 StackOverflow :P 提前谢谢大家。

private void EditActionPerformed(java.awt.event.ActionEvent evt) {                                     

   try{
       int row = Table_customer.getSelectedRow();
    String TClick = (Table_customer.getModel().getValueAt(row, 0).toString());
       String edit1 = lNaam.getText();
       String edit2 = lEmail.getText();
       String edit3 = lAdres.getText();
       String edit4 = lPlaats.getText();
       String edit5 = lPostcode.getText();
       String edit6 = lTelefoon.getText();
       String edit7 = lBedrijfsNaam.getText();

       String sql="update customers set naam='"+edit1+"',email='"+edit2+"',adres='"+edit3+"',plaats='"+edit4+"',postcode='"+edit5+"',telefoon='"+edit6+"',bedrijfsnaam='"+edit7+"'where KL_NR='"+TClick+"'";
       preparedStatement=connect.prepareStatement(sql);
       preparedStatement.execute();
    JOptionPane.showMessageDialog(null, "Updated");
   TheRefresher();


   }catch(SQLException | HeadlessException e){
   JOptionPane.showMessageDialog(null, e);
   }finally {
try{ //Update_tableCustomers();
resultSet.close(); preparedStatement.close(); }
catch(Exception e) { } }


}                     

附言。此外,如果我编辑另一行并返回到先前编辑的行,它工作得很好,当我连续两次编辑同一行时就会出现问题。

4

2 回答 2

0

它看起来像这一行:

 String TClick = (Table_customer.getModel().getValueAt(row, 0).toString());

因为堆栈跟踪显示了这一点:

at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)

所以 getValueAt 抛出异常,可能是因为 getSelectedRow 返回 -1。

检查以确保 getSelectedRow 正在执行您期望的操作。

看到这个:<JTable>.getSelectedRow() 正在返回 -1

于 2013-10-10T12:13:33.363 回答
0

可能在此操作之前您没有选择任何行。

private void EditActionPerformed(java.awt.event.ActionEvent evt)        
{                                     

  try{
   int row = Table_customer.getSelectedRow();
   System.out.println("selected row : " + row);
   if(row < 0) {
      JOptionPane.showMessageDialog(parentDialog, "Must select row" ); 
      return;
   }
    ...
 }
于 2013-10-10T12:16:57.400 回答