在eclipse中使用JavaFX空指针异常加载css [英] Loading css using JavaFX null pointer exception in eclipse

查看:190
本文介绍了在eclipse中使用JavaFX空指针异常加载css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这行代码将CSS文件加载到JavaFX中,它给了我一个空指针异常:

I'm trying to load a CSS file into JavaFX using this line of code and it gives me a null pointer exception:

scene.getStylesheets().add(welcome.class.getResource("background.css").toExternalForm());

我的 background.css 位于与我所做的欢迎课程相同的文件夹。

My background.css is located in the same folder as the welcome class I have made.

我知道为什么会得到一个空指针?

Any idea why I get a null pointer?

错误日志:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at welcome.start(welcome.java:164)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source)
    ... 1 more


推荐答案

任何资源都应该在类路径上才能成功加载(如果它与欢迎类位于同一个文件夹中,那么它已经是如此)。然后你应该在样式表文件的路径前面加上'/'符号,这样看起来像这样:

Any resource should be on the classpath to be loaded successfully (if it is in the same folder as you welcome class then it is already so). Then you should precede the path to the stylesheet file by '/' symbol, so that it looks like this:

scene.getStylesheets().add(welcome.class.getResource("/background.css").toExternalForm());

然后它会成功加载。

这篇关于在eclipse中使用JavaFX空指针异常加载css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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