gpt4 book ai didi

java - Jtable ArrayIndexOutOfBounds异常-1

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

我使用 java netbeans 开发了我的系统,其中有一个 JTable。每次当我启动我的程序时,我的 JTable 都会显示我的 sqlite 中的所有数据。然后,当我用鼠标单击数据时,它将在我的 JtextField 中显示详细信息。我上面还有一个菜单栏。一切都工作正常,除了每当我启动系统时,如果首先我没有用鼠标单击我的 JTable,而是只是使用菜单栏,然后​​按我的 jtable,它会显示错误。 ArrayIndexOutOfBoundsEception -1。仅当我按照上述操作时才会发生此异常。除此之外,如果我只需再次单击鼠标就可以了。我只是想知道如何解决这个问题

我打印了它,下面是错误:

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 Customer.table_customerMouseClicked(Customer.java:703)
at Customer.access$500(Customer.java:23)
at Customer$6.mouseClicked(Customer.java:377)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6508)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
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:4501)
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)

这是我从 sqlite 加载数据并将其显示在 JTextField 中的代码

 private void table_customerMouseClicked(java.awt.event.MouseEvent evt) {                                            
conn=javaconnect.ConnectDB();



panel_1.setVisible(false);

txt_custname.setEditable(false);
ComboBox1.setEditable(false);
txt_proID.setEditable(false);
txt_pricePer.setEditable(false);
txt_quantity1.setEditable(false);
txt_total.setEditable(false);
txt_email.setEditable(false);
txt_company.setEditable(false);

cmd_delete.setEnabled(true);
cmd_edit.setEnabled(true);

try{
int row = table_customer.getSelectedRow();
String table_click = (table_customer.getModel().getValueAt(row, 0).toString());
String sql ="select* from Customer where CustID='"+table_click+"' ";
pst = conn.prepareStatement(sql);
rs=pst.executeQuery();

if(rs.next()){
String add1 = rs.getString("Name");
txt_custname.setText(add1);

String add2 = rs.getString("Email");
txt_email.setText(add2);

String add3 = rs.getString("Company");
txt_company.setText(add3);

String add4 = rs.getString("ProductName");
ComboBox1.setSelectedItem(add4);

String add5 = rs.getString("ProID");
txt_proID.setText(add5);

String add6 = rs.getString("PricePerUnit");
txt_pricePer.setText(add6);

String add7 = rs.getString("Quantity");
txt_quantity1.setText(add7);

String add8 = rs.getString("Total");
txt_total.setText(add8);

String add9 = rs.getString("CustID");
txt_custID.setText(add9);

}

String sql1= "select * from Product where Name = '"+ComboBox1.getSelectedItem()+"'";
pst = conn.prepareStatement(sql1);

rs=pst.executeQuery();

if (rs.next()){
String add1 = rs.getString("ProID");
txt_proID.setText(add1);

String add2 = rs.getString("PricePerUnit");
txt_pricePer.setText(add2);

String add3 = rs.getString("Quantity");
txt_quantity.setText(add3);

String add4 = rs.getString("StockOut");
txt_stock.setText(add4);
}


}

catch(Exception e){
e.printStackTrace();
}

finally{
try{
rs.close();
pst.close();
conn.close();
}
catch(Exception e){}
}
}

最佳答案

当 JTable 中没有选择行时调用 mouseClicked 函数时,就会出现问题。在这种情况下,

 row = table_customer.getSelectedRow();
如果没有选择任何行,

将返回-1。之后你正在做

table_customer.getModel().getValueAt(row, 0)

这显然会抛出 ArrayIndexOutOfBoundsException 异常。

关于java - Jtable ArrayIndexOutOfBounds异常-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23973212/

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