[Fixed! There was a typo error. I wrote productsTable1
when it was supposed to be productsTable2
]
I'm trying to assign the values of a JTable's row (which was created from a 3d array) to an array, invoiceDump
, after I click on the row, but I get an error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.swing.JTable.getValueAt(int, int)" is null
when I selected the 1st row and this error Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
when I select the 2nd row. Here's is what I did
private void productsTable2MouseClicked(java.awt.event.MouseEvent evt) {
int[] selectRow = productsTable2.getSelectedRows(); // array that stores selected row's data
// Displays the selected row's data to text fields
invoiceDump[0] = productsTable1.getValueAt(selectRow[0], 0).toString();
invoiceDump[1] = productsTable1.getValueAt(selectRow[0], 1).toString();
invoiceDump[2] = productsTable1.getValueAt(selectRow[0], 2).toString();
invoiceDump[3] = productsTable1.getValueAt(selectRow[0], 3).toString();
invoiceDump[4] = productsTable1.getValueAt(selectRow[0], 4).toString();
invoiceDump[5] = productsTable1.getValueAt(selectRow[0], 5).toString();
invoiceDump[6] = productsTable1.getValueAt(selectRow[0], 6).toString();
System.out.println(Arrays.toString(invoiceDump));
}
I replaced int[] selectRow = productsTable2.getSelectedRows();
with int selectRow = productsTable2.getSelectedRow();
, but it generated the same errors. This block of code works fine when I want to set text fields from the row's values. However, I want to get the values directly from the table and assign it to my array. How to make it work?