0

I have created a game that when started, a start page/ window is displayed with the option to exit or start the game. A second window should open on the start button click but it gives the following error:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null

I have read up on this error but no suggestions seem to work. This is my main class with the start method:

package game;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.File;
import java.net.URL;

public class AdventureGame extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("EBRUTATEN - The Curse of Methuselah");
        URL url = new File("F:\\[0] Programming 3.1\\[0] CAPSTONE ASSIGNMENT\\[0] Current Working File\\MyJavaFXGame2\\src\\fxml\\sample.fxml").toURI().toURL();
        Parent root = FXMLLoader.load(url);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String...args) {
        launch(args);
    }
}

And this is the start button method in its controller class where the button action is handled

    public void startButClicked() {
        startBut.setOnAction(event -> {
            Parent gameOutput = null;
            try {
                URL url = new File("F:\\[0] Programming 3.1\\[0] CAPSTONE ASSIGNMENT\\[0] Current Working File\\MyJavaFXGame2\\src\\fxml\\game.fxml").toURI().toURL();
                gameOutput = FXMLLoader.load(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene gameView = new Scene(gameOutput);
            Stage secStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
            secStage.setScene(gameView);
            secStage.show();
            mediaPlayerStarterMusic.stop();
        });
    }

Any advice would be appreciated.

  • I have read this question and I used it to make some changes but those changes cause another error: javafx.fxml.LoadException: /F:/%5B0%5D%20Programming%203.1/%5B0%5D%20CAPSTONE%20ASSIGNMENT/%5B0%5D%20Current%20Working%20File/MyJavaFXGame2/src/game/game.fxml:13 How do I fix them both? I keep getting one or the other. – Spicy Tiger Jun 09 '20 at 09:33
  • please read the answer again: as first step, you __must__ pin-point the exact location of the error. As second step, isolate it into a [mcve] and keep changing the small example until it does work. Then add a bit more complexity from your project .. rinse and repeat - that's called debugging a life of a programmer :) BTW, don't add details in comments, instead edit your question – kleopatra Jun 09 '20 at 10:17
  • The only way that `gameOutput` is null when you create the `gameView` scene is if there is a previous exception, whose stack trace you are displaying in the catch block. You need to [read that stack trace](https://stackoverflow.com/questions/3988788/) and figure out what's causing that exception. It's likely that the path to the FXML file is wrong: while you can make it work using File URLs, it's [not the correct approach](https://stackoverflow.com/questions/61531317). – James_D Jun 09 '20 at 11:33
  • @SpicyTiger the part of the error you posted in the comment indicates that there is an issue with line 13 of your fxml file. There's much info missing to help you with that part; in particular the fxml file was not posted and there's more info in the stacktrace than just the info about which line contains the issue. – fabian Jun 09 '20 at 16:49

0 Answers0