I am using JavaFX to show a view from my game.
The View is loaded when I call the method in my MainApp class:
public class MainApp extends Application {
//fields
public MainApp() {
this.game = new Game();
}
//lots of other methods
public void showGameView() {
try {
System.out.println(game.getPlayer().getCurrentRoom());
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/GameView.fxml"));
AnchorPane GameView = (AnchorPane) loader.load();
rootLayout.setCenter(GameView);
GameViewController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Game getGame() {
return game;
}
The Game object stores some information and stuff. The controller looks the following:
public class GameViewController {
private MainApp mainApp;
@FXML
public void initialize() {
mainApp.getGame(). ... //do something else
}
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
}
I have always done it that way. When the controller gets loaded, the MainApp objects gets set in the controller and I can work with that. But now I get a Nullpointer when any mainApp.get... gets called. The field mainApp is null. I dont really know what the deal is here, because as I said it worked like this in other projects.