You are getting a NullPointerException
because when you retrieve the information from your data storage, you do not check whether or not it is null
. So when you call model.getValueAt(i,j)
, this is most likely returning a null
value at some point. You do not check this, so you are calling toString()
on a null
value (null.toString()
), which will cause your code to crash with a NullPointerException
. I would suggest you use a turnary
operator. This will check whether or not the value returned is null
first, and then create the String s
depending on whether or not it was null
.
String s = ((model.getValueAt(i,j) == null) ? null : model.getValueAt(i,j).toString());
This will set your String
to null
if the record is null
, otherwise set it to the record's value. OR, if you would like it to be an empty String
(""
) when the record does not exist, you can use:
String s = ((model.getValueAt(i,j) == null) ? "" : model.getValueAt(i,j).toString());