我该如何纠正以下问题?
以下代码适用于具有4列的JTable,而不会触发空指针异常:
public void setSubject() {
try {
int row = table_SPAssignList.getSelectedRow();
String prop_addr1_ = (table_SPAssignList.getModel().getValueAt(row, 0)).toString();
String prop_id_ = (table_SPAssignList.getModel().getValueAt(row, 3)).toString();
/*
* UPDATE TriTier to set Subject Property
*/
dbConn = sqliteConnection.dbConnector();
String query = "UPDATE TABLENAME SET sp_id = " + prop_id_ + " where id = " + tID;
PreparedStatement pst = dbConn.prepareStatement(query);
pst.execute();
pst.close();
/*
* Display Subject Property Information on Panel
*
*/
String addr = prop_addr1_ ;
label__SPAssignDBID.setText(prop_id_);
label_SPAssignAddress.setText(addr);
} catch (final Exception getProperty) {
getProperty.printStackTrace();
}
}
但是,如果我在getValueAt(row,0)和getValueAt(row,3)代码行之间插入以下代码,如果该字段不包含数据,则似乎会出现空指针错误:
String prop_addr2_ = (table_SPAssignList.getModel().getValueAt(row, 1)).toString();
String prop_zip_ = (table_SPAssignList.getModel().getValueAt(row, 2)).toString();
数据库中的这些字段很可能没有任何数据。 table_SPAssignList
对象的第0列和第3列将始终包含值。
我正在连接到SQLite数据库。
以下帖子可能与我的问题有关
(update data from jTable to database -null pointer exception error)和(getValueAt() method returns null)