简体   繁体   English

如何从具有空单元格的jtable获取字符串

[英]how to get string from jtable with null cell

I have jtable and the data from mysql. 我有jtable和来自mysql的数据。 In mysql I have a column that allowed for null (it's called 'fil'). 在mysql中,我有一个允许为null的列(称为“ fil”)。 When i retrive the data to jtable, it's OK. 当我将数据检索到jtable时,就可以了。 Then, I want to fill the jtextarea and enable the button if the 'fil' is not null by click the jtable row. 然后,我想填充jtextarea并通过单击jtable行来启用“ fil”不为null的按钮。 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: 问题是当我单击带有空'fil'的行时,程序给出错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
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: 您无法在空对象上调用toString(),因此您需要执行以下操作:

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

Then you would use your original test: 然后,您将使用原始测试:

If (fil != "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM