I feel like I'm missing something easy but I'm not sure what it is. Here is my code:

@FXML
    private Label label;
    private Button startGameButton;

    @FXML
    private void startGame (ActionEvent event) {
        label.setText("Ok. Let's begin.");
        startGameButton.setVisible(false);
    }

I made a button in Scene Builder and set its fx:id to startGameButton. I'm just trying to make the startGameButton disappear when I click on it. I thought this would be sufficient but I'm getting a

Caused by: java.lang.NullPointerException
    at myjavafx2.FXMLDocumentController.startGame(FXMLDocumentController.java:29)

I feel as though the NullPointerException is telling me the button doesn't exist but I made it in Scene Builder. I don't think I would need to do Button startGameButton = new Button(); because Scene Builder should be doing that for me. What am I missing?

有帮助吗?

解决方案

You have to add @FXML before your Button as well.
Like this:

@FXML
private Label label;
@FXML
private Button startGameButton;

@FXML
private void startGame (ActionEvent event) {
    label.setText("Ok. Let's begin.");
    startGameButton.setVisible(false);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top