简体   繁体   中英

Javafx ExceptionInInitializerError

The previous posts that I have seen haven't worked for me. Is the error because I haven't used any fxml loader. But it didn't work in any fxml file. Can you help me find the error and link me some resources to better understand all the errors similar to this. I keep getting similar errors. Thanks in advance.

My Main.java

package sample;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.animation.TranslateTransition;

import javafx.scene.*;
import javafx.util.Duration;

import java.awt.event.ActionEvent;

public class Main extends Application {

        private final static javafx.scene.image.Image tree1 = new javafx.scene.image.Image(Main.class.getResource("/images/Tree-1.png").toString());
        private final static javafx.scene.image.Image tree2 = new javafx.scene.image.Image(Main.class.getResource("/images/2.png").toString());
        private final static javafx.scene.image.Image tree3 = new javafx.scene.image.Image(Main.class.getResource("/images/3.png").toString());
        private final static javafx.scene.image.Image tree4 = new javafx.scene.image.Image(Main.class.getResource("/images/4.png").toString());
        private final static javafx.scene.image.Image tree5 = new javafx.scene.image.Image(Main.class.getResource("/images/5.png").toString());
        private final static javafx.scene.image.Image tree6 = new javafx.scene.image.Image(Main.class.getResource("/images/6.png").toString());
        private final static javafx.scene.image.Image tree7 = new javafx.scene.image.Image(Main.class.getResource("/images/7.png").toString());
        private final static javafx.scene.image.Image tree8 = new javafx.scene.image.Image(Main.class.getResource("/images/8.png").toString());




  private Group tree;
    @Override
    public void start(Stage primaryStage) throws Exception{


        final ImageView t1 = new ImageView(tree1);
        final ImageView t2 = new ImageView(tree2);
        final ImageView t3 = new ImageView(tree3);
        final ImageView t4 = new ImageView(tree4);
        final ImageView t5 = new ImageView(tree5);
        final ImageView t6 = new ImageView(tree6);
        final ImageView t7 = new ImageView(tree7);
        final ImageView t8 = new ImageView(tree8);

        tree= new Group(t1);
        tree.setTranslateX(200);
        tree.setTranslateY(220);

        Timeline t = new Timeline();
        t.setCycleCount(Timeline.INDEFINITE);

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(200),
                event ->{

                    tree.getChildren().setAll(t2);
                    }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(300),
                event ->
        {
            tree.getChildren().setAll(t3);
        }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(400),
               event ->
        {
            tree.getChildren().setAll(t4);
        }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(500),
                 event ->
        {
            tree.getChildren().setAll(t5);
        }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(600),
                event ->
        {
            tree.getChildren().setAll(t6);
        }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(700),
                 event ->
        {
            tree.getChildren().setAll(t7);
        }));

        t.getKeyFrames().add(new KeyFrame(
                Duration.millis(800),
                event ->
        {
            tree.getChildren().setAll(t8);
        }));
        t.play();

        primaryStage.setScene(new Scene(tree,700,800));
        primaryStage.setTitle("Learning Animation");
        primaryStage.show();

    }


    public static void main(String[] args) {

        Application.launch(args);
        launch(args);

    }
}

ERROR:

"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" "-

javaagent:C:\Users\Eli\AppData\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\lib\idea_rt.jar=7355:C:\Users\Eli\AppData\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_131\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_131\jre\lib\rt.jar;F:\pomodoro\out\production\pomodoro" sample.Main
    Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$156(LauncherImpl.java:352)
        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)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.NullPointerException
        at sample.Main.<clinit>(Main.java:21)
        ... 11 more
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
    Caused by: java.lang.NullPointerException
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)

This happens because in this line

 private final static javafx.scene.image.Image tree1 = new javafx.scene.image.Image(Main.class.getResource("/images/Tree-1.png").toString());

you referring to the path, which default FX loader could not find

you should check where you are storing your images, but here example structure, which is work perfectly

example structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM