I have this controller class for showing a database query in a TableView, but i am having error of NullPointerException with the \"setCellValueFactory(new PropertyValueFactory\"
One of the instance fields in your controller is lacking an @FXML
annotation. Since the field is private, the FXML loader is unable to inject the control reference into the field during loading. Here are your instance field declarations:
@FXML
private TextField txtCampo,txtCampo2;
@FXML
private Button btAdicionar,btConsultar;
@FXML
private TableView<Pessoa> tabValues;
@FXML
private TableColumn<Pessoa, Integer> tbcCod;
private TableColumn<Pessoa, String>tbcNome;
Notice that the last field, tbcNome, is not annotated. As a result, when your initialize method is called, the tbcNome
field contains a null
reference, resulting in the exception.
To fix your problem, all you may need to do is add the @FXML
annotation to the instance field declaration for tbcNome
.
You may have encouraged this error by adopting the habit of listing more than one variable in your type declarations, eg. private Button btAdicionar, btConsultar;
. In my opinion, this is a bad habit that can encourage errors like this to happen. I would suggest that you try to adopt the coding style in which each instance field has its own type declaration statement.