在JavaFX中将数据设置为TableView时是否为空指针异常?

时间:2016-01-15 15:22:54

标签: java javafx tableview

当我将数据设置为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(温度);

1 个答案:

答案 0 :(得分:0)

在使用data之前,请使用data = new ObservableList<ObservableList>();

初始化列表