Adhi Foo Adhi Foo - 6 months ago 45
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 != "") {
jButton1.setEnabled(true);
jButton1.setToolTipText(fil);
} else {
jButton1.setEnabled(false);
jButton1.setToolTipText("");
}
jTextArea1.setText("Subject: " + sub + "\n" + "Description: " + "\n" + desc + "\n" + "From " + start + " to " + end + "\n" + "Status: " + sta);

jLabel3.setText(num);


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
if (fil != "") {

should be:

if (fil != null) {

Edit:

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 != "")