1

Before you guys gave me another link to look at, I have already researched my case for 2 days and had no luck :(

The question I have is how to declare the ImageView/AudioClip/MediaPlayer without getting the error message

First, let me show you the error message:

Exception in thread "main" java.lang.reflect.InvocationTargetException

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)

Caused by: java.lang.ExceptionInInitializerError
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    ... 5 more

Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main
    at javafx.graphics/com.sun.javafx.tk.Toolkit.checkFxUserThread(Toolkit.java:291)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(QuantumToolkit.java:424)
    at javafx.graphics/javafx.stage.Stage.<init>(Stage.java:254)
    at javafx.graphics/javafx.stage.Stage.<init>(Stage.java:240)
    at YuanGeometryWar/Output.Main.<clinit>(Main.java:18)
    ... 11 more

I know that this problem is caused because of the declaration of image:

How the line of code is now (Give me error):

URL background = this.getClass().getResource("resources/Image/MenuBackGround.jpg");
ImageView menuBack = new ImageView(new Image(background.toString()));

mainPane.getChildren().add(menuBack);

How it was before (something similar to this and it works):

try(InputStream pathIntro = Files.newInputStream(Paths.get("resources/Image/ControlBack.png"))){
        intro = new ImageView(new Image(pathIntro));

        controlPane.getChildren().add(intro);
    }

I am also having the same problem with getting audioClip or MediaPlayer to work.

I just want to know why is this causing me a problem and what is the proper way to achieve something like this?

The resources folder is not in src and is directly under the project folder

Alex Yuan
  • 35
  • 5
  • 4
    Well, the stack trace you've provided shows the following as the fundamental cause: `Caused by: java.lang.IllegalStateException: Not on FX application thread; currentThread = main`. As you can see, you're executing the problematic code on the wrong thread; execute said code on the _JavaFX Application Thread_ instead. For more specific help, please [edit] your question to provide a [mre]. – Slaw Dec 28 '19 at 06:30

0 Answers0