0

[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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kuromiu
  • 31
  • 7
  • 1
    Post your [mre] demonstrating the problem. So you need to create a JFrame with a JTable with a couple rows/columns of hard coded data. Then add the MouseListener to the table and load the data into the array. Once we get the MRE working then you can fix your real application code. The point of the MRE is to simplify the problem so we can see exactly what you are doing. – camickr Apr 10 '21 at 04:10
  • Would help to see your table model so we know how you implemented getValueAt. – MarsAtomic Apr 10 '21 at 04:33
  • @MarsAtomic the table is created in Netbeans IDE, so no code was written for it. – Kuromiu Apr 10 '21 at 05:20
  • @camickr I created a different JFrame with predetermined values in each cell and tried my code again. Tt did not generate the error that I have in my main project, and I have not used a MouseListener since I couldn't really find a use for it in the testing JFrame. I think the problem is either a user error (since I forgot to fill in a cell in the table, which generates the same error) or maybe the 3d array I made to generate the contents of the table? – Kuromiu Apr 10 '21 at 06:01
  • @camickr Turns out that I made a typo in writing the table variable. Thanks for the suggestion though! – Kuromiu Apr 10 '21 at 06:07
  • 1
    *Turns out that I made a typo in writing the table variable* - which is another reason why you create an [mre]. It allows you to simplify the problem often resulting in you finding the problem in your original. code. – camickr Apr 10 '21 at 14:53

0 Answers0