The convertCombo() in my initialize method causes a null pointer exception, and I am not sure why. Is there any better way to convert my Objects User in the combo box so its a readable String, but still keep it as an object in the combobox so I can pass the ID from my object for later use.
public class SelectionController implements Initializable {
@FXML
private ComboBox<User> combobox;
@FXML
private Button selectButton;
@Override
public void initialize(URL arg, ResourceBundle arg2) {
try {
Connection conn = DBConnecter.getConnection();
ResultSet rs = conn.createStatement().executeQuery("SELECT id,fname FROM students");
ObservableList<User> data = FXCollections.observableArrayList();
if(conn != null) {
System.out.println("connected");
}
while(rs.next()) {
data.add(new User(rs.getInt("id"),rs.getString("fname")));
}
combobox.setItems(data);
convertCombo();
} catch (Exception e) {
e.printStackTrace();
}
}
public void convertCombo() {
combobox.setConverter(new StringConverter<User>() {
@Override
public User fromString(String arg) {
return null;
}
@Override
public String toString(User arg) {
return arg.getName();
}
});
}
}