0
嘗試從initialize()方法內訪問Button時,出現NullPointerException。嘗試訪問JavaFX按鈕時出現NullPointerException
我已經標記了下面這行會導致異常。
public class MyController implements Initializable {
@FXML
public Button connect;
public MyController() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Layouts/ClientLayout.fxml"));
loader.setController(this);
Parent root = loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root, 460, 470));
stage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
connect.setOnAction(e -> { // this line causes the nullpointerexception
connect.setDisable(true);
});
}
}
你確定ClientLayout.fxml有fx:id的按鈕與連接? – subash