我觉得我错过了一些简单的东西,但我不确定它是什么。这是我的代码:
@FXML
private Label label;
private Button startGameButton;
@FXML
private void startGame (ActionEvent event) {
label.setText("Ok. Let's begin.");
startGameButton.setVisible(false);
}
我在Scene Builder中创建了一个按钮,并将 fx:id 设置为 startGameButton 。当我点击它时,我只是想让 startGameButton 消失。我认为这就足够了,但我得到了
Caused by: java.lang.NullPointerException
at myjavafx2.FXMLDocumentController.startGame(FXMLDocumentController.java:29)
我觉得好像NullPointerException告诉我按钮不存在但是我在Scene Builder中创建了它。我不认为我需要做Button startGameButton = new Button();
,因为Scene Builder应该为我做这件事。我错过了什么?
答案 0 :(得分:2)
您必须在@FXML
之前添加Button
。@FXML
private Label label;
@FXML
private Button startGameButton;
@FXML
private void startGame (ActionEvent event) {
label.setText("Ok. Let's begin.");
startGameButton.setVisible(false);
}