我创建了一个游戏,该游戏在启动时会显示一个开始页面/窗口,并带有退出或启动游戏的选项。单击“开始”按钮时应打开第二个窗口,但会出现以下错误:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null
我已经阅读了此错误,但似乎没有建议。这是我使用start方法的主要课程:
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);
}
}
这是其控制器类中的开始按钮方法,用于处理按钮动作
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();
});
}
任何建议将不胜感激。