Ayoub
Ayoub

Reputation: 1

Exception in JavaFX Application start method

I'm using Eclipse to write some JavaFX code, and when I will try run this code:

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

The eclipse give me this error:

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

The operation system is "Windows 10".

And this snapshot for my project files.

enter image description here

So can anyone help me?

Upvotes: 0

Views: 927

Answers (2)

Ala Eddine Menai
Ala Eddine Menai

Reputation: 2880

Because you try to load your image as an external file ,but your image is in your source folder .

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.

so you can load it just with line of code :

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

Upvotes: 1

ItachiUchiha
ItachiUchiha

Reputation: 36792

You are not supposed to load a resource on your classpath using File. You can directly use the getResource() to load the resource.

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

Upvotes: 4

Related Questions