Java:应用程序启动方法中出现异常 java.lang.reflect.InitationTargetException

标签 java intellij-idea javafx fxml

我一直在尝试从事 Simon Says 项目,但运行该项目时不断出现错误。我正在使用 IntelliJ、Java 13.0.2 和最新版本的 JavaFX。

错误是:

Exception in Application start method
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 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x229b412d) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x229b412d
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at Main.start(Main.java:15)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application Main

我的主要内容是:

import javafx.application.Application;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {
   private Stage primaryStage;
   private AnchorPane rootLayout;

   public void start(Stage primaryStage) {
      try {
         this.primaryStage = primaryStage;
         primaryStage.setTitle("Simon Game - java ripoff edition");
         FXMLLoader loader = new FXMLLoader(Main.class.getResource("GUI.fxml"));
         Parent root = loader.load();
         Scene scene = new Scene(this.rootLayout);
         primaryStage.setScene(scene);
         primaryStage.show();
         scene.getStylesheets().add(this.getClass().getResource("application.css").toExternalForm());
      } catch (Exception var4) {
         var4.printStackTrace();
      }

   }

   public static void main(String[] args) {
      launch(args);
   }
}

我的 FXML 代码是:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" fx:1d="root" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: silver;" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <children>
      <Button fx:id="startBtn" layoutX="466.0" layoutY="140.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="47.0" prefWidth="99.0" text="Start">
         <font>
            <Font name="Gill Sans MT" size="19.0" />
         </font>
      </Button>
      <Rectangle fx:id="greenBtn" arcHeight="5.0" arcWidth="5.0" fill="#0ab916" height="150.0" layoutX="121.0" layoutY="51.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
      <Rectangle fx:id="redBtn" arcHeight="5.0" arcWidth="5.0" fill="#ff1f1f" height="150.0" layoutX="271.0" layoutY="51.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
      <Rectangle fx:id="yellowbtn" arcHeight="5.0" arcWidth="5.0" fill="#e8eb23" height="150.0" layoutX="121.0" layoutY="201.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
      <Rectangle fx:id="blueBtn" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="150.0" layoutX="271.0" layoutY="201.0" onMouseClicked="#userInput" stroke="BLACK" strokeType="INSIDE" width="150.0" />
      <TabPane layoutX="37.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" />
      <Button fx:id="instructBtn" layoutX="466.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="47.0" prefWidth="99.0" text="Instructions">
         <font>
            <Font name="Gill Sans MT" size="16.0" />
         </font>
      </Button>
   </children>
</AnchorPane>

我还有其他可以使用按钮的类,但我只从主类中收到错误。我们将不胜感激所有帮助!

最佳答案

您需要导入所有必需的包。

关于Java:应用程序启动方法中出现异常 java.lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60514724/

相关文章:

java - Powermock - java.lang.IllegalStateException : Failed to transform class

java - 为什么要使用 HashMap ?

java - 获取包 javax.ws.rs 不存在错误

java - 当我尝试运行 java 代码时,无法找到或加载主类

java - 如何在 Intellij 的同一个 jar 中导出 JavaFX 应用程序和依赖项?

java - 将 JAR 作为 Windows 服务运行

java - 谷歌应用程序引擎 JSP

grails - 在 IntelliJ 中使用 Grails 2.4.3

javafx - 如何在 JavaFX 中正确从路径中减去形状

java - 如何为组合框和列表中的选项添加标签?