0

I want to change scenes on my stage or close stage when I push the startgame button and exitgame button, but its just show me first scene, and when I'm trying to push one of this buttons compiler close in case of NullPointerException.

I also have main class and two fxml files, but I think it's don't necessary to put it here, it contains just two anchor panes, primitive labels and buttons.

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable{

    private Stage stage = new Stage();

    private FXMLLoader loader = new FXMLLoader();

    private AnchorPane anchorPane = new AnchorPane();

    private Scene scene = new Scene(new AnchorPane());

    @FXML private Button startGameButton;
    @FXML private Button endGameButton;

    @FXML private Button buttonAnswer1;
    @FXML private Button buttonAnswer2;
    @FXML private Button buttonAnswer3;
    @FXML private Button buttonAnswer4;

    public void createScene(int typeOfScene) throws Exception
    {
        if (typeOfScene == 1)
        {
            loader = new FXMLLoader(getClass().getResource("/sample/sample.fxml"));
            anchorPane = loader.load();

            scene = new Scene(anchorPane);
            stage.setScene(scene);
            stage.show();
        }

        if (typeOfScene == 2)
        {
            scene = null;

            loader = new FXMLLoader(getClass().getResource("/sample/episodesFXML.fxml"));

            anchorPane = loader.load();

            scene = new Scene(anchorPane);
            stage.setScene(scene);
            stage.show();
        }
    }

    public void getPrimaryStage(Stage stage) throws Exception
    {
        this.stage = stage;
        createScene(1);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        startGameButton.setOnAction(event -> {
            try {
                createScene(2);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        });

        endGameButton.setOnAction(event -> {
            stage.close();
        });
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Terra Incognita
  • 167
  • 2
  • 2
  • 11
  • 3
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – hotzst Mar 25 '16 at 21:05
  • Indeed this is a duplicate of that question unless you can provide additional details... e.g. which value is `null`... What does the stacktrace say? – fabian Mar 25 '16 at 21:17
  • So when i push button start game i call function `createScenes(2)` my compiler is angry on line `anchorPane = loader.load();` and when i push button end game is nothing happend, but it must close my stage. – Terra Incognita Mar 25 '16 at 21:34

1 Answers1

0

I remember a problem like that, fxml file is linked to a controller file by the fx:controller attribute. You try to load a fxml in a controller that it's not handled/loaded. Create another form and showing/hiding forms seems simpler.

Tokazio
  • 516
  • 2
  • 18
  • Remove fx:controller attribute from the fxml files and use fxmlLoader.setController(); to dynamically define the controller. – Tokazio Mar 25 '16 at 21:45