为什么我在启动简单代码 (JavaFX) 时遇到问题?



我开始使用JavaFXml,我在编译程序时遇到问题。编译器向我展示问题,我没有任何想法来解决这个问题。

我的我的班级:

package com.biku.readerFX;
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 {
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);
}
}

我的控制器类:

package com.biku.readerFX;
public class Controller {
}

我的样本.fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.biku.readerFX.Controller">
<Label text="I love bacon"/>
<Button text="Submit"/>
</VBox>

当我编译这个时,编译者向我展示了一个错误:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java --module-path /home/biku/Pobrane/openjfx-13/javafx-sdk-13/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -javaagent:/snap/intellij-idea-community/177/lib/idea_rt.jar=41763:/snap/intellij-idea-community/177/bin -Dfile.encoding=UTF-8 -classpath /home/biku/IdeaProjects/WordReader/target/classes:/usr/lib/jvm/java-1.11.0-openjdk-amd64:/home/biku/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar:/home/biku/.m2/repository/org/apache/maven/maven-plugin-api/3.0/maven-plugin-api-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model/3.0/maven-model-3.0.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-plexus/1.4.2/sisu-inject-plexus-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.2/sisu-inject-bean-1.4.2.jar:/home/biku/.m2/repository/org/sonatype/sisu/sisu-guice/2.1.7/sisu-guice-2.1.7-noaop.jar:/home/biku/.m2/repository/org/apache/maven/maven-artifact/3.0/maven-artifact-3.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-utils/2.0.4/plexus-utils-2.0.4.jar:/home/biku/.m2/repository/org/apache/maven/maven-core/3.0/maven-core-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings/3.0/maven-settings-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-settings-builder/3.0/maven-settings-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-repository-metadata/3.0/maven-repository-metadata-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-model-builder/3.0/maven-model-builder-3.0.jar:/home/biku/.m2/repository/org/apache/maven/maven-aether-provider/3.0/maven-aether-provider-3.0.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-impl/1.7/aether-impl-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-spi/1.7/aether-spi-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-api/1.7/aether-api-1.7.jar:/home/biku/.m2/repository/org/sonatype/aether/aether-util/1.7/aether-util-1.7.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-classworlds/2.2.3/plexus-classworlds-2.2.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar:/home/biku/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-utils/3.2.1/maven-shared-utils-3.2.1.jar:/home/biku/.m2/repository/commons-io/commons-io/2.5/commons-io-2.5.jar:/home/biku/.m2/repository/org/apache/maven/shared/maven-shared-incremental/1.1/maven-shared-incremental-1.1.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-java/0.9.10/plexus-java-0.9.10.jar:/home/biku/.m2/repository/org/ow2/asm/asm/6.2/asm-6.2.jar:/home/biku/.m2/repository/com/thoughtworks/qdox/qdox/2.0-M8/qdox-2.0-M8.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.8.4/plexus-compiler-api-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-manager/2.8.4/plexus-compiler-manager-2.8.4.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-compiler-javac/2.8.4/plexus-compiler-javac-2.8.4.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-controls/12.0.2/javafx-controls-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-graphics/12.0.2/javafx-graphics-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2.jar:/home/biku/.m2/repository/org/openjfx/javafx-base/12.0.2/javafx-base-12.0.2-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13.jar:/home/biku/.m2/repository/org/openjfx/javafx-fxml/13/javafx-fxml-13-linux.jar:/home/biku/.m2/repository/org/openjfx/javafx-maven-plugin/0.0.3/javafx-maven-plugin-0.0.3.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-archiver/3.6.0/plexus-archiver-3.6.0.jar:/home/biku/.m2/repository/org/codehaus/plexus/plexus-io/3.0.1/plexus-io-3.0.1.jar:/home/biku/.m2/repository/org/apache/commons/commons-compress/1.16.1/commons-compress-1.16.1.jar:/home/biku/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/home/biku/.m2/repository/org/iq80/snappy/snappy/0.4/snappy-0.4.jar:/home/biku/.m2/repository/org/tukaani/xz/1.8/xz-1.8.jar:/home/biku/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/home/biku/.m2/repository/org/twdata/maven/mojo-executor/2.3.0/mojo-executor-2.3.0.jar:/home/biku/.m2/repository/org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar:/home/biku/.m2/repository/org/slf4j/slf4j-simple/1.7.22/slf4j-simple-1.7.22.jar com.biku.readerFX.Main
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:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at com.biku.readerFX.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application com.biku.readerFX.Main
Process finished with exit code 1

我尝试像在此页面上一样解决它:

  • https://github.com/openjfx/javafx-maven-plugin
  • https://openjfx.io/openjfx-docs/#maven

并将虚拟机选项路径添加到 fx: --module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml

将像 lib 这样的 javafx 添加到项目中。

你有什么想法来帮助我吗?

无论以何种方式设置项目,以下行:

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

无法获取"sample.fxml"。确保获取文件的位置/路径规范正确,否则将始终遇到此错误消息(位于堆栈跟踪的底部(:

Caused by: java.lang.NullPointerException: Location is required.

tl;大卫:你的"sample.fxml"道路是错误的。您可能必须将其(sample.fxml文件(移动到与Main.java文件相同的包中,或者可能是Maven设置的src/main/resources/目录中。我对 Gradle 并不完全熟悉,但您还必须将 fxml 文件放在资源根目录中。

好的,我有解决方案,路径是正确的,但我没有文件"Project_Name".iml。此文件必须与 pom.xml 相同级别,直接位于项目文件夹中。在这个文件中是这样的:

<?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_11">
<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$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>

最新更新