[英]JAVAFX - Exception caused by Location is not set when loading fxml file
我正在嘗試使用以下代碼將fxml文件集成到我的項目中,
final FXMLLoader fxmlLoader =
new FXMLLoader(this.getClass().getResource("/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
程序在第二行崩潰,拋出此異常,
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/1199823423.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 FileActions.start(FileActions.java:42)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/970544503.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1878510174.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/1792840102.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/1671111378.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Process finished with exit code 130
我也嘗試使用確切的路徑(使用復制路徑),但是仍然遇到相同的錯誤。
也嘗試過
Parent root = FXMLLoader.load(getClass().getResource("../resources/sample.fxml"));
//and
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
並將其放在我的Java文件的同一文件夾中,但沒有任何作用...
我不知道這是否相關,但這是我的iml文件,
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" exported="" name="commons-net-3.6" level="project" />
<orderEntry type="library" exported="" name="hamcrest-core-1.3" level="project" />
<orderEntry type="library" exported="" name="junit-4.12" level="project" />
</component>
</module>
是什么原因造成的,我該如何解決?
謝謝,
亨利
該錯誤似乎表明fxml
文件是從錯誤的位置加載的。 getClass().getResource
加載資源,根據資源的每個Java規范,該資源由包含子字符串序列的字符串標識,該字符串由(/)
分隔,后跟資源名稱。 每個子字符串必須具有一個有效的Java標識符。 沒有真正保證使用..
進行資源解析,因為它不是有效的標識符。
假設您沒有使用Maven文件夾結構,那么我建議您簡單地將fxml
文件與啟動它的Java類放在相同的程序包中,並將路徑設置為"sample.fxml"
。輸出getClass().getClassLoader().getResource("sample.fxml")
時會看到什么getClass().getClassLoader().getResource("sample.fxml")
另外,請確保您的文件名與資源名稱匹配,在文件名前后都沒有空格-您可以重命名並檢查空格(如果有)。
我看到您正在使用Maven和Jetbrains。 當代碼中fxml文件的位置錯誤時,確實會發生此異常。 您正在使用Maven。 Maven在其resources文件夾中搜索資源(例如fxml文件)。 Maven項目的根文件夾是pom.xml所在的位置。 我將其稱為{mavenRoot}。 因此,假設您的fxml文件在以下路徑中:
{mavenRoot}/src/main/resources/fxml/sample.fxml
然后,您可以使用theese的兩行代碼來設置fxml加載程序:
FXMLLoader loader1 = new FXMLLoader();
loader1.setLocation(getClass().getResource("/fxml/sample.fxml"));
在maven項目中,getResource()將搜索{mavenRoot}/src/main/resources
文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.