java - Cannot get data from JTable because of NullPointer Execption -
This code is an action listener for a button. I have JTable that the user enters the data. It is filled with integer. For some reason, I'm able to println (each one of the values) though, I have to store them in my ArrayList due to a NullPointerException Not Enabling
btnCalculate.addActionListener (New ActionListener) {@Override Public Null ActionPerformed (ActionEvent arg0) {ArrayList. & Lt; Integer> Blade = New ArrayList & lt; Integer & gt; (); DefaultTableModel DTM = (DefaultTableModel) tableBlades.getModel (); Integer Nrow = dtm.getRowCo For Unt (), nCol = dtm.getColumnCount (); println (Nrow + "," + nCol); (int i = 0; I & lt; nrow; i ++) {(for int j = 0; J & lt; nCol; J ++) {blades.add (Integer.parseInt (dtm.getValueAt (i, j) .tar ()));}}}});
(Edit: Added brackets for readability)
Depending on the error log, I am able to narrow down the line where I add value Array list I know that getValueAt () returns an object, so I used to make it string () and parseInt () to make it an integer any idea what here
Do you know how to use a debugger? If not, you need to know what is going on through the code and how to take action.
dtm.getValue (i, j)
j
when this is happening is . With a debugger you can find out where this is happening.
If this is expected (it can be null
), then you should program defensively and enter a redundant test such as:
integer cell value = dtm.getValueAt (i, j); If (cellar! = Null) {blade.ed (cell value); }
Of course, if this is not good for cell value, then null
, or blade
, you should handle that error Should, for example, report back to the user that the data is unavailable, etc.
Comments
Post a Comment