【问题标题】:NPE caused by Modena.class.getResource("TestApp.css")Modena.class.getResource("TestApp.css") 引起的 NPE
【发布时间】:2014-01-16 17:01:13
【问题描述】:

我正在尝试运行可在here 找到的 java-fx 示例 Modena。我在 Windows 8 上使用 Netbeans 8.0 Beta,JDK8 最新版本。我想知道为什么我什至无法编译它。

事实上发生的第一个例外就是这个。

我注意到自己是 JavaFX 的新手。

程序中的异常行是:

private static final String testAppCssUrl = Modena.class.getResource("TestApp.css").toExternalForm();

我断言我已验证并且该文件存在。并且示例 jar 文件可以在我的计算机上使用我上面解释的 Java 默认配置运行。

Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at com.sun.javafx.application.LauncherImpl$2.run(LauncherImpl.java:330) at com.sun.javafx.application.LauncherImpl$2.run(LauncherImpl.java:330)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331) at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
    at modena.Modena.<clinit>(Modena.java:100)
    ... 13 more
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:362)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    ... 5 more

【问题讨论】:

  • getResource("TestApp.css") 返回 NULL。所以你得到一个NPE。您刚刚为其提供了一个文件名,但它可能需要实际的文件路径来加载文件。
  • @BrianC 不,将路径更改为完整路径不起作用。正如我在答案中所说的那样,问题在于使用 Shift+F6 而不是 F6 运行。

标签: java netbeans javafx javafx-8


【解决方案1】:

问题在于仅使用Shift+F6 运行主文件。我应该使用F6 来运行项目。 F6 运行项目,而Shift+F6 只运行包含静态 main 方法的文件,不考虑项目配置。

【讨论】:

    猜你喜欢
    • 2013-09-08
    • 1970-01-01
    • 2017-12-02
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多