Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FXMLLoader在尝试JUnit测试时抛出NullPointerException,但在编译和正常运行时则不会_Java_Junit_Fxml - Fatal编程技术网

Java FXMLLoader在尝试JUnit测试时抛出NullPointerException,但在编译和正常运行时则不会

Java FXMLLoader在尝试JUnit测试时抛出NullPointerException,但在编译和正常运行时则不会,java,junit,fxml,Java,Junit,Fxml,我有一个名为setNewScene的方法,它调用FXMLLoader: public void setNewScene(String newScene) throws IOException { Scene scene = new Scene(FXMLLoader.load(Main.class.getResource(newScene)), 800, 650); primaryStage.setScene(scene); primaryStage.setOnCloseR

我有一个名为setNewScene的方法,它调用FXMLLoader:

public void setNewScene(String newScene) throws IOException {
    Scene scene = new Scene(FXMLLoader.load(Main.class.getResource(newScene)), 800, 650);
    primaryStage.setScene(scene);
    primaryStage.setOnCloseRequest(event -> System.exit(0));
}
在同一类(engine.Main)中,使用以下命令调用它:

setNewScene("/view/fxml/mainView.fxml");
在正常编译后调用时,这里没有问题。然而,当我尝试在依赖于engine.Main的类上运行JUnit测试时,我得到“java.lang.NullPointerException:Location是必需的”

我该如何解决这个问题

Exception in Application start method

java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$2/1637070917.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3211)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at engine.Main.setNewScene(Main.java:288)
at engine.Main.start(Main.java:84)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/242831779.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/19308827.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$50/980064585.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/58915055.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1280903300.run(Unknown Source)
... 1 more


Process finished with exit code -1