我使用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){}
}
}
答案 0 :(得分:1)
当JTable中没有选择行时,调用mouseClicked函数时会出现问题。在这种情况下,
row = table_customer.getSelectedRow();
如果未选择任何行,将返回-1
。之后你正在做
table_customer.getModel().getValueAt(row, 0)
显然会抛出ArrayIndexOutOfBoundsException
例外。