当我将数据设置为ObservableList
抛出空指针异常时,我无法理解该问题。有人请帮我找到解决方案。
这是我的代码:
@FXML
private Label label;
@FXML
private ObservableList<ObservableList> data;
@FXML
private TableColumn column1;
@FXML
private TableColumn column2;
@FXML
private TableView table;
public void initialize(URL url, ResourceBundle rb) {
// TODO
String selectSql = "select first_name,last_name from hr.employees";
try (Connection conn = ConnectDB.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(selectSql)) {
System.out.println("Getting Connection....");
while (rs.next()) {
ObservableList<String> temp = FXCollections.observableArrayList();
temp.add(rs.getString(1));
System.out.println("First Name == "+rs.getString(1));
System.out.println("Last Name == "+rs.getString(2));
temp.add(rs.getString(2));
System.out.println("List Value === "+temp);
data.add(temp);
//table.setItems(temp);
}
table.setItems(data);
} catch (Exception e) {
//System.out.println("Error: "+e.getMessage());
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, e);
}
}
我收到错误---&gt; data.add(温度);
答案 0 :(得分:0)
在使用data
之前,请使用data = new ObservableList<ObservableList>();