0

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?

2 Answers 2

4

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()));
Sign up to request clarification or add additional context in comments.

Comments

1

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

5 Comments

it is worked for me ,when i need to load image just i write path witout using getResource
Did you move the generated JAR-file to a different location and executed it? Why would new Image() lookup in the current classloader? Would be new to me.
Indeed ... wasn't aware. Quoting the documentation of Image: ` If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case. ` Maybe you want to add this reference inside your answer aswell.
Okey i will do .Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.