2017-08-27 57 views
0

我使用Eclipse編寫一些JavaFX代碼,當我將嘗試運行此代碼:異常在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

因此,誰能幫助我?

回答

4

您不應該使用File在您的類路徑中加載資源。您可以直接使用getResource()加載資源。

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

因爲您嘗試將圖像作爲外部文件加載,但圖像位於源文件夾中。

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

All URLs supported by URL can be passed to the constructor. If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.

所以你可以用一行代碼加載:

// 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")); 
+0

被編譯 – FibreFoX

+0

當它爲我工作,這是行不通的,當我需要加載圖像只是我沒有使用getResource編寫路徑 –

+0

您是否將生成的JAR文件移動到其他位置並執行它?爲什麼'new Image()'會在當前的類加載器中查找?對我來說是新的。 – FibreFoX

相關問題