JavaFX Application start方法中的异常

时间:2017-08-27 17:55:39

标签: javafx

我正在使用Eclipse编写一些JavaFX代码,当我尝试运行此代码时:

ImageView imgView = new ImageView(new Image(new File(getClass()
    .getResource("Things\\back.jpg").toExternalForm()).toURI().toString()));

日食给了我这个错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at application.OnOffDikra.<init>(OnOffDikra.java:55)
    at application.ToggleSwitch.<init>(ToggleSwitch.java:20)
    at application.Main.CreateContent(Main.java:19)
    at application.Main.start(Main.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application application.Main

操作系统是“Windows 10”。

这是我项目文件的快照。

enter image description here

那么有人可以帮助我吗?

2 个答案:

答案 0 :(得分:4)

您不应该使用File在类路径上加载资源。您可以直接使用getResource()加载资源。

ImageView imgView = new ImageView(
        new Image(getClass().getResource("/Things/back.jpg").toExternalForm()));

答案 1 :(得分:1)

因为您尝试将图像作为外部文件加载,但您的图像位于源文件夹中。

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html

  

URL支持的所有URL都可以传递给构造函数。如果   传递的字符串不是有效的URL,而是一个路径,而不是Image   在那种情况下在类路径上搜索。

所以你可以用代码行加载它:

// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath

    ImageView imageView = new ImageView(new Image("/Things/back.jpg"));