You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans中移动FXML文件至其他包后FXMLLoader无法找到文件的问题求助

NetBeans中移动FXML文件至其他包后FXMLLoader无法找到文件的问题求助

我正在用NetBeans开发一个JavaFX项目,最近碰到了FXML文件加载失败的问题。原本我的VentanaLogin.fxml文件放在practica6.main包下面,为了让项目结构更清晰,我把它移到了新建的practica6.vista包中,结果运行时就报错了。

下面是控制台输出的错误信息:

Updating property file: C:\Users\Usuario\Documents\NetBeansProjects\Practica6_Parte3\build\built-jar.properties
Compiling 1 source file to C:\Users\Usuario\Documents\NetBeansProjects\Practica6_Parte3\build\classes
compile-single:
run-single:
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:569)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1091)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3374)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3337)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3305)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3277)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3246)
    at practica6.main.main.start(main.java:37)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application practica6.main.main
C:\Users\Usuario\Documents\NetBeansProjects\Practica6_Parte3\nbproject\build-impl.xml:1367: The following error occurred while executing this line:
C:\Users\Usuario\Documents\NetBeansProjects\Practica6_Parte3\nbproject\build-impl.xml:962: Java returned: 1
BUILD FAILED (total time: 2 seconds)

目前FXML文件的路径是src/practica6/vista/VentanaLogin.fxml,我在Main类中是这样加载它的:

FXMLLoader.load(getClass().getResource("/practica6/vista/VentanaLogin.fxml"));

我的项目包结构大概是这样的:

  • practica6.main:存放Main类
  • practica6.vista:存放VentanaLogin.fxml文件

我已经尝试过清理项目然后重新构建了,但还是报同样的错。有没有大佬知道问题出在哪?或者我在移动FXML文件的时候可能漏掉了什么步骤吗?

备注:内容来源于stack exchange,提问作者JavaMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 09:53:01