加载FXML文件时“需要位置”exception

我正在尝试加载FXML文件并将其显示为应用程序窗口,但我得到一个例外。 FXML文件由FXML场景生成器创建。

以下是该课程的代码

public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setScene(FXMLLoader.load(getClass().getResource("sample.fxml"))); primaryStage.show(); } } 

和FXML文件

         

这是我得到的例外

 Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157) at com.sun.javafx.application.LauncherImpl$$Lambda$1/2074407503.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:3201) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091) at Pass4D.start(Pass4D.java:19) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821) at com.sun.javafx.application.LauncherImpl$$Lambda$51/317090070.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323) at com.sun.javafx.application.PlatformImpl$$Lambda$47/1833150059.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292) at com.sun.javafx.application.PlatformImpl$$Lambda$49/2115863517.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291) at com.sun.javafx.application.PlatformImpl$$Lambda$48/1436737924.run(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 

我究竟做错了什么?

ps这里是项目结构

在此处输入图像描述

简短的回答是getClass().getResource("sample.fxml")如果在运行时类路径上找不到资源, getClass().getResource("sample.fxml")null提示返回null ,而不是当前目录等。

所以这取决于您的IDE项目设置,如果您正在使用eclipse尝试添加sample.fxml驻留在运行配置中的文件夹。

一些想法……

  • 尝试使用getClass().getResource("/sample.fxml")代替……
  • 尝试将sample.fxml移动到resources文件夹中。 我对你的IDE知之甚少,但我怀疑这个文件夹只用于.java文件……对于eclipse中的gradle项目来说肯定是这样 – 资源必须只在src/main/resources树中被添加到运行时类路径中……

我今天已经发布了这个,所以又来了,希望它可以帮到你。

这是一个适用于开发环境,Scene Builder和打包JAR的解决方案。

文件夹结构:

在此处输入图像描述

Main.java:

 package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; public class Main extends Application { @Override public void start(Stage primaryStage) { try { FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/RootLayout.fxml")); AnchorPane rootLayout = (AnchorPane) loader.load(); Scene scene = new Scene(rootLayout, 400, 400); scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } } 

RootLayout.fxml:

               

RootLayoutController.java:

 package application.view; import javafx.fxml.FXML; import javafx.scene.control.Button; public class RootLayoutController { @FXML Button sunButton; @FXML public void handleSunButtonClick() { System.out.println( "Button clicked"); } } 

toolbar.css:

 .sun-button { -fx-graphic: url('./icons/sun.png'); } 

application.css:

 .root { -fx-background-color:lightgray; } 

sun.png:

在此处输入图像描述

这适用于开发环境和打包JAR(在Eclipse中选择“将所需库提取到生成的JAR中”)。

屏幕截图(只是一个带有通过css加载图标的按钮)

在此处输入图像描述

从oracle尝试这个例子:

  @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml")); Scene scene = new Scene(root, 300, 275); stage.setTitle("FXML Welcome"); stage.setScene(scene); stage.show(); } 

Main.class.getResource为我工作。 我试图从子类而不是Main类获取资源。 我在那里使用getClass()。getResource()指向当前的类。 我没有在资源目录中的FXML文件。