加载FXML时出现NullPointerException [英] NullPointerException when loading an FXML

查看:107
本文介绍了加载FXML时出现NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到有关我的FXML位置的NullPointerException.但是,只有在升级JavaFX版本后才开始发生这种情况(我需要13才能满足项目要求).

I am getting a NullPointerException regarding the location of my FXML. However, this only started to happen after I upgraded my JavaFX version (I need 13 to meet project requirements).

我的主班:

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws IOException {

    String path = "/fxml/loginScene.fxml";

    Parent root = FXMLLoader.load(getClass().getResource(path));
    primaryStage.initStyle(StageStyle.UNDECORATED);

    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/imgs/icon.png")));
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}
}

模块信息:

module main {
requires javafx.controls;
requires javafx.fxml;
requires jfoenix;
requires vlcj;

opens me.fullcam.mosaicov2 to javafx.fxml;
exports me.fullcam.mosaicov2;
}

我的目录:

例外情况:

    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:567)
    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:567)
    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:835)
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 main/me.fullcam.mosaicov2.Main.start(Main.java:20)

我的build.gradle :

    plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.jlink' version '2.12.0'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'uk.co.caprica', name: 'vlcj', version: '4.2.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    compile group: 'com.google.http-client', name: 'google-http-client', version: '1.23.0'
    compile group: 'com.jfoenix', name: 'jfoenix', version: '8.0.9'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.28'
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

mainClassName = "$moduleName/me.fullcam.mosaicov2.Main"

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'mosaico-desktop'
    }
}

我认为一切都井井有条,但是没有将资源添加到类路径中.我还没有找到类似的东西.我正在使用IntelliJ.

I think that everything is in order, but resources aren't being added to the classpath. I have not found anything like that. I'm using IntelliJ.

我尝试了./gradlew clean --info run,但这是我的堆栈跟踪:

I've tryied ./gradlew clean --info run, but that's my stacktrace:

> Task :run FAILED
Task ':run' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Starting process 'command '/usr/lib/jvm/java-12-oracle/bin/java''. Working directory: /home/leonardo/Modelos/IdeaProjects/FullCam/Desktop/mosaico-desktop-v2 Command: /usr/lib/jvm/java-12-oracle/bin/java --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media --module-path /home/leonardo/Modelos/IdeaProjects/FullCam/Desktop/mosaico-desktop-v2/build/classes/java/main:/home/leonardo/Modelos/IdeaProjects/FullCam/Desktop/mosaico-desktop-v2/build/resources/main:/root/.gradle/caches/modules-2/files-2.1/uk.co.caprica/vlcj/4.2.0/727b20426956174d64c6817367f45080ad95a4d0/vlcj-4.2.0.jar:/root/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.0.1/1d160beb3f8924d5b98d03d94a149021146f71cc/jackson-databind-2.0.1.jar:/root/.gradle/caches/modules-2/files-2.1/com.google.http-client/google-http-client/1.23.0/8e86c84ff3c98eca6423e97780325b299133d858/google-http-client-1.23.0.jar:/root/.gradle/caches/modules-2/files-2.1/com.jfoenix/jfoenix/8.0.9/71da4db8303be22f6366df8933dd93519398c8f8/jfoenix-8.0.9.jar:/root/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/1.7.28/cf5f2cf3c31e0e41b68d932d756398a1238d4456/slf4j-simple-1.7.28.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-fxml/13/2c1016c361ce5797927e9e14847fbca9bebf3bd5/javafx-fxml-13-linux.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/13/bcedf8eb6e11c2a7908b3126004d41274f6334f3/javafx-controls-13-linux.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-controls/13/6cd7169d2a729846c35fba9dc65af23a10ca253b/javafx-controls-13.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-media/13/d3626ef7290012a1e55a32532977ed78edc02548/javafx-media-13-linux.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/13/d853d12684f60037dfeea1a47e61396e0febfdfd/javafx-graphics-13-linux.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-graphics/13/a9407212df2b75d557a509ec14a9e8e282494b4e/javafx-graphics-13.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/13/828b1e1a35104bb0f26a347c2716d553ca4cf4d2/javafx-base-13-linux.jar:/root/.gradle/caches/modules-2/files-2.1/org.openjfx/javafx-base/13/43c52e1d11b38514e9d2421ad98ca6a35de12b0/javafx-base-13.jar:/root/.gradle/caches/modules-2/files-2.1/uk.co.caprica/vlcj-natives/4.1.0/d45eb15ac25a1c085a365166d430435bd990409d/vlcj-natives-4.1.0.jar:/root/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.0.1/36e5996abe8655a6471b82af5ac1cd6786aecf85/jackson-annotations-2.0.1.jar:/root/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.0.1/481d14ec21e034ed45217c4854dbbf15940ce108/jackson-core-2.0.1.jar:/root/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/1.3.9/40719ea6961c0cb6afaeb6a921eaa1f6afd4cfdf/jsr305-1.3.9.jar:/root/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpclient/4.0.1/1d7d28fa738bdbfe4fbd895d9486308999bdf440/httpclient-4.0.1.jar:/root/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.28/2cd9b264f76e3d087ee21bfc99305928e1bdb443/slf4j-api-1.7.28.jar:/root/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna-platform/5.2.0/5520c6f3382801576547dd20854225ae2899b649/jna-platform-5.2.0.jar:/root/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna/5.2.0/ed8b772eb077a9cb50e44e90899c66a9a6c00e67/jna-5.2.0.jar:/root/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.0.1/e813b8722c387b22e1adccf7914729db09bcb4a9/httpcore-4.0.1.jar:/root/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.1.1/5043bfebc3db072ed80fbd362e7caf00e885d8ae/commons-logging-1.1.1.jar:/root/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.3/fd32786786e2adb664d5ecc965da47629dca14ba/commons-codec-1.3.jar --patch-module main=/home/leonardo/Modelos/IdeaProjects/FullCam/Desktop/mosaico-desktop-v2/build/resources/main --module main/me.fullcam.mosaicov2.Main -Dfile.encoding=UTF-8 -Duser.country=BR -Duser.language=pt -Duser.variant main/me.fullcam.mosaicov2.Main
Successfully started process 'command '/usr/lib/jvm/java-12-oracle/bin/java''
/home/leonardo/Modelos/IdeaProjects/FullCam/Desktop/mosaico-desktop-v2/.
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:567)
        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:567)
        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:835)
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 main/me.fullcam.mosaicov2.Main.start(Main.java:22)
        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(AccessController.java:389)
        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 me.fullcam.mosaicov2.Main
:run (Thread[Task worker for ':',5,main]) completed. Took 0.574 secs.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-12-oracle/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
5 actionable tasks: 5 executed

推荐答案

基于发布的 MVCE项目,这些是使项目正常运行所需的一些修改:

Based on the posted MVCE project, these are a few modifications that are required to make the project work:

build.gradle

基于此:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.jlink' version '2.12.0'
}

group 'me.project.desktop'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'uk.co.caprica', name: 'vlcj', version: '4.2.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    compile group: 'com.google.http-client', name: 'google-http-client', version: '1.23.0'
    compile group: 'com.jfoenix', name: 'jfoenix', version: '8.0.9'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.28'
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.base', 'javafx.graphics', 'javafx.media' ]
}

mainClassName = "me.project.mosaic.Main"

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'mosaico-desktop'
    }
}

所需的更改是:

  • 将Java级别设置为11

  • Set Java level to 11

更新依赖项

JFoenix设置为8.0.9,适用于Java 1.8.如果您使用Java 11+运行,则会出现异常:

JFoenix is set to 8.0.9, that is intended for Java 1.8. If you run with Java 11+, you will get an exception:

Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.converters.PaintConverter
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)

由于JDK 9是CSS转换器,所以它是公共API,因此您需要9的JFoenix,例如9.0.9.

Since JDK 9, CSS converters are public API, so you need JFoenix for 9, like 9.0.9.

请参阅此帖子以供参考.

  • 使用JDK 11,而不是12或13

JFoenix的JFXTextField控件具有常备的问题,并使用反射来访问私有API.

JFoenix's JFXTextField control has an standing issue with the use of reflection to access private API.

java.lang.IllegalAccessException: class com.jfoenix.skins.JFXTextFieldSkin cannot access a member of class javafx.scene.control.skin.TextFieldSkin (in module javafx.controls) with modifiers "private"
        at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
...
java.lang.IllegalAccessException: class com.jfoenix.skins.JFXTextFieldSkin cannot access a member of class javafx.scene.control.skin.TextFieldSkin (in module javafx.controls) with modifiers "private"
        at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
        at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639)
...  

并导致空指针异常:

Caused by: java.lang.NullPointerException
        at com.jfoenix.skins.JFXTextFieldSkin.updateTextPos(JFXTextFieldSkin.java:109)

与Java 11一起运行,效果很好.

Running with Java 11, it works fine.

作为参考,请参见此帖子.

  • 添加出口

JFoenix使用私有API,因此它需要大量导出,这些导出必须添加到VM参数中,例如:

JFoenix uses private API, so it requires a bunch of exports, that have to be added to the VM arguments, like:

run {

    jvmArgs = [

        "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED",

        "--add-exports=javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED",

        "--add-exports=javafx.base/com.sun.javafx.binding=ALL-UNNAMED",
        "--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED",
        "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED"
    ]

}

有关参考,请参见这将是生成的build.gradle文件:

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'org.beryx.jlink' version '2.12.0'
}

group 'me.project.desktop'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'uk.co.caprica', name: 'vlcj', version: '4.2.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    compile group: 'com.google.http-client', name: 'google-http-client', version: '1.23.0'
    compile group: 'com.jfoenix', name: 'jfoenix', version: '9.0.9'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.28'
}

javafx {
    version = "13"
    modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.base', 'javafx.graphics', 'javafx.media' ]
}

run {
    jvmArgs = ["--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED",
        "--add-exports=javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED",
        "--add-exports=javafx.base/com.sun.javafx.binding=ALL-UNNAMED",
        "--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED",
        "--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED"
    ]
}

mainClassName = "me.project.mosaic.Main"

jlink {
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'mosaico-desktop'
    }
}

运行(使用JDK 11):

Running (with JDK 11):

./gradlew clean run

将起作用.

模块化项目

为了运行jlink,您需要创建一个模块化描述符,例如:

In order to run jlink, you need to create a modular descriptor, like:

module mosaic.viewer {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.jfoenix;
    requires vlcj;
    requires jackson.annotations;
    requires jackson.databind;
    requires jackson.core;
    requires google.http.client;
    requires org.slf4j;
    requires jsr305;

    opens me.project.mosaic.login to javafx.fxml;
    exports me.project.mosaic;
}

  • 打开包裹
  • 由于FXML使用反射,因此必须将带有控制器的软件包打开到javafx.fxml模块.在这种情况下:opens me.project.mosaic.login to javafx.fxml;.

    Since FXML uses reflection, you have to open to the javafx.fxml module the package(s) with the controller(s). In this case: opens me.project.mosaic.login to javafx.fxml;.

    • 添加出口

    由于您现在有了命名模块,所以--add-exports现在设置为com.jfoenix而不是ALL-UNNAMED.

    Since you have a named module now, the --add-exports are now set to com.jfoenix instead of ALL-UNNAMED.

    运行(使用JDK 11):

    Running (with JDK 11):

    ./gradlew clean jlink
    

    将创建一个自定义图像,您将能够从启动器脚本中运行生成的图像:

    will create a custom image, and you will be able to run the generated image from the launcher script:

    build/image/bin/mosaic-desktop
    

    IDE

    如果从IDE(IntelliJ)而不是从终端运行项目,请确保使用IntelliJ的Gradle窗口,该窗口将向您显示项目和可用任务.

    If you run the project from your IDE (IntelliJ) and not from a terminal, make sure you use the Gradle window for IntelliJ, that will show you the project and available tasks.

    在项目结构"中设置>"非常重要,将项目SDK和语言级别设置为11.

    It is important to set in Project Structure -> Project the project SDK and language level to 11.

    此外,Gradle窗口具有一个设置按钮,单击它,并确保Gradle JVM也设置为JDK 11.

    Also, the Gradle window has a settings button, click on it, and make sure that the Gradle JVM is also set to JDK 11.

    您可以从此窗口运行:

    • 任务->应用程序->运行

    • Tasks -> application -> run

    任务->构建-> jlink

    Tasks -> build -> jlink

    最后,您还可以根据上述任务创建基于Gradle的运行配置.

    Finally, you can also create Run configurations, but based on Gradle, with the above tasks.

    这篇关于加载FXML时出现NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆