大家好我在运行此代码时有一个NullPointerException,它指向model =(DefaultTableModel)DbUtils.resultSetToTableModel(rs),我已经导入了net.proteanit.sql.DbUtils,任何想法?
public void setTableValues(){
try {
cn.start();
String sql = "select id,first_name as Firstname,last_name as Lastname,email,phone,description as AllergyInfo from clients where isDeleted=0";
rs = cn.executeSelect(sql);
model = (DefaultTableModel) DbUtils.resultSetToTableModel(rs);
jTable1.setModel(model);
rowSorter = new TableRowSorter<>(jTable1.getModel());
jTable1.setRowSorter(rowSorter);
cn.stop();
} catch (SQLException ex) {
Logger.getLogger(Clients.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:2)
使用像eclipse这样的任何IDE来调试代码以添加断点并了解哪个变量为空并且指向空指针异常。
您可以在代码中添加空检查以防止空指针异常,如
If(object!= null)