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.