NullPointerException in JavaFX initialize in TableView

末鹿安然 提交于 2019-12-02 02:46:22

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!