0

When I am setting data to the ObservableList it throwing null pointer exception, I could not understand the problem. anyone please help me to find the solution.

Here is my code:

@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);
    }
}

i am getting error in ---> data.add(temp);

Thirunavukkarasu
  • 208
  • 6
  • 26
  • In which line does the exception raise? You have two ObservableList s one: ObservableList temp = FXCollections.observableArrayList(); and the other: is ObservableList data Please post the stacktrace – pleft Jan 15 '16 at 15:28
  • i am getting error in ---> data.add(temp); – Thirunavukkarasu Jan 15 '16 at 15:32
  • Probably the @FXMLprivate ObservableList data; does not work for ObservableList type and your data object is not instantiated. Maybe you wanted to write something like this: ObservableList data = table.getItems() – pleft Jan 15 '16 at 15:37
  • thank you, i am not initialized the data, so that it's showing error. but right now the tableview loading with empty. – Thirunavukkarasu Jan 15 '16 at 15:43

1 Answers1

1

Before using data, initialize the list with data = new ObservableList<ObservableList>();

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256