Adhi Foo Adhi Foo - 1 year ago 129
Java Question

how to get string from jtable with null cell

I have jtable and the data from mysql.
In mysql I have a column that allowed for null (it's called 'fil').
When i retrive the data to jtable, it's OK.
Then, I want to fill the jtextarea and enable the button if the 'fil' is not null by click the jtable row.
Here my code:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int row = jTable1.getSelectedRow();
String num = jTable1.getValueAt(row, 0).toString();
String sub = jTable1.getValueAt(row, 1).toString();
String desc = jTable1.getValueAt(row, 2).toString();
String start = jTable1.getValueAt(row, 3).toString();
String end = jTable1.getValueAt(row, 4).toString();
String sta = jTable1.getValueAt(row, 5).toString();
String fil = jTable1.getValueAt(row, 6).toString();
if (fil != "") {
} else {
jTextArea1.setText("Subject: " + sub + "\n" + "Description: " + "\n" + desc + "\n" + "From " + start + " to " + end + "\n" + "Status: " + sta);


the problem is when I clicked the row with null 'fil', the program give the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Answer Source
if (fil != "") {

should be:

if (fil != null) {


String fil = jTable1.getValueAt(row, 6).toString();

You can't invoke toString() on a null object so you need something like:

Object filObject = jTable1.getValueAt(row, 6);
String fil = (filObject == null) ? "" : filObject.toString();

Then you would use your original test:

If (fil != "")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download