NullPointerException in JavaFX initialize in TableView

前端 未结 1 375
傲寒
傲寒 2021-01-26 23:00

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\"

相关标签:
1条回答
  • 2021-01-26 23:58

    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.

    0 讨论(0)
提交回复
热议问题