AsySyah
AsySyah

Reputation: 21

Jtable ArrayIndexOutOfBounds Exception -1

I have developed my system using java netbeans where inside it have a JTable. Everytime when i start my program my JTable will show all the data from my sqlite. Then when i mouseclick the data it will show the details in my JtextField. i also have a menu bar on top of it. Everything is working fine except whenever i start my system if first i did not mouse click my JTable but instead i just play around with my menu bar then i press on my jtable it will show an error. ArrayIndexOutOfBoundsEception -1. This exception will only occur if i did as mention. Apart from that if i just mouseclick it again then it will be fine. Ijust want to know how to rectify this problem

I have print stack it and below is the error:

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)

This is my code for loading the data from my sqlite and show it in my 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){}
   }
}      

Upvotes: 0

Views: 259

Answers (1)

Vikram Singh
Vikram Singh

Reputation: 1816

Your problem occurs when your mouseClicked function is called when there is no row selected in the JTable. In this case,

 row = table_customer.getSelectedRow();

will return -1 if no row is selected. After that you are doing

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

which will obviously throw the ArrayIndexOutOfBoundsException exception.

Upvotes: 1

Related Questions