JavaFX和maven:NullPointerException:Location is required
JavaFX和maven:NullPointerException:Location is required
我一直在尝试使用Maven和JavaFX进行设置。尽管我对Maven和JavaFX不熟悉,但我没有预料到会这么具有挑战性。我的Java知识相当扎实(包括Swing),没想到设置起来会遇到这么多困难。
我从IntelliJ 13.0 Community Edition提供的JavaFX项目开始。我的Main类中的代码相对较少:
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { //getClass().getResource("../../resources/sample.fxml"); Parent root = FXMLLoader.load(getClass().getResource("../sample.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 300, 275)); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
我的pom.xml
也不大:
4.0.0 sample JavaFXDemo 1.0-SNAPSHOT jar RubberDucky com.zenjava javafx-maven-plugin 2.0 com.zenjava javafx-maven-plugin 2.0 sample.Main com.oracle javafx 2.2 ${java.home}/lib/jfxrt.jar system
我正在使用JavaFX Maven Plugin来构建应用程序。运行mvn clean jfx:jar
后,一切似乎都很顺利,所有构建都成功。但当我尝试运行应用程序时,出现以下错误:
Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.javafx.main.Main.launchApp(Main.java:698) at com.javafx.main.Main.main(Main.java:871) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So urce) at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source) at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NullPointerException: Location is required. at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at sample.Main.start(Main.java:15) at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source) at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source) at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source) ... 1 more
经过一些粗略的调试,问题似乎出在我加载文件的路径上。当我将sample.fxml
硬编码到硬盘上的某个位置时,应用程序可以正常运行。从IntelliJ中运行应用程序时,当前设置(如上所示)也是如此。
我觉得我已经用尽了所有的资源(包括StackOverflow和一些非常相似的错误),但我就是找不到问题出在哪里。