【问题标题】:InvocationTargetException when running a javafx program运行 javafx 程序时出现 InvocationTargetException
【发布时间】:2014-06-01 16:12:28
【问题描述】:

所以当我的电脑有 jdk 1.7.0 时,这在 javafx 的示例中有效,所以这可能是 java8 中 FX 的新版本;

我得到了一个不错的堆栈跟踪

jfx-project-run:
Executing E:\workspace\PathFinderApp\dist\run1095471771\PathFinderApp.jar using platform C:\Program Files\Java\jdk1.8.0\jre/bin/java
Exception in Application start method
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:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    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:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:744)
Caused by: javafx.fxml.LoadException: 
file:/E:/workspace/PathFinderApp/dist/run1095471771/PathFinderApp.jar!/com/rpg/gui/main.fxml:11

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)
    at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:937)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:976)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:738)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2723)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3191)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3164)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3140)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3120)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3113)
    at com.rpg.gui.GUI.loadMainPane(GUI.java:34)
    at com.rpg.gui.GUI.initialize(GUI.java:20)
    at Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
    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.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    ... 1 more
Caused by: java.lang.InstantiationException: com.rpg.gui.MainController
    at java.lang.Class.newInstance(Class.java:418)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
    ... 26 more
Caused by: java.lang.NoSuchMethodException: com.rpg.gui.MainController.<init>()
    at java.lang.Class.getConstructor0(Class.java:2971)
    at java.lang.Class.newInstance(Class.java:403)
    ... 28 more
Exception running application Main
Java Result: 1

基本上告诉我这里出了点问题"com/rpg/gui/main.fxml:11"

那一行是

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.rpg.gui.MainController">

所以我可以弄清楚它不喜欢与主控制器有关,所以它必须与加载 fxml 和控制器有关,对吧?但这就是我的想法结束的地方

MainController mainController = new MainController(path);
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
Window.setMainController(mainController);
Window.swap(path+"content.fxml");

这就是它向任何有 javaFX 经验或知道 java8 中的任何更改的人抛出所有内容的方法??

【问题讨论】:

  • 我对 JavaFX 一无所知,但查看堆栈跟踪似乎它希望 MainController 有一个无参数构造函数(它没有)。
  • 什么是com.rpg.gui.GUI.loadMainPane(GUI.java:34)?显示代码老兄
  • @parsaporahmad 我假设GUI.java:34Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));,现在包含在 OP 中。
  • 我摆脱了构造函数,我现在使用 setPath(path) 但它抱怨没有看到 content.fxml 的位置,我应该编辑上面的帖子吗?我的意思是从技术上讲它解决了这个问题
  • 我认为这与从 JavaFX2.2 更改为 Java 8 没有任何关系:您的代码也会失败。

标签: java netbeans javafx


【解决方案1】:

您的MainController 没有零参数构造函数。如果 FXMLLoader 在根元素上遇到 fx:controller 属性,它会尝试通过(有效地)调用该属性中指定的类的零参数构造函数来创建该控制器的实例。

要解决此问题(最简单的方法),从 FXML 文件中删除 fx:controller 属性,并在 FXMLLoader 上“手动”设置控制器。你需要创建一个FXMLLoader 实例,而不是依赖静态的load(...) 方法:

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(new MainController(path));
Pane mainPane = loader.load();

【讨论】:

  • 以上代码中的FXMLLoadergetClass()是什么?我是外汇新手,请帮忙
  • @InzimamTariqIT 修正了错字。
  • 好的,我的代码现在可以在没有这个的情况下工作。我只是清理并构建项目,一切都很好。
  • 如果我们删除 fx:controller 属性并手动添加控制器,我们需要手动添加所有操作,因为如果 fxml 文件中没有控制器的引用,则显示按钮和其他内容的更多错误我们在 xml 中定义动作。
  • @InzimamTariqIT 不,您不应该这样做。它的行为方式与您在 FXML 中声明控制器的方式完全相同。
【解决方案2】:

我遇到了同样的问题,想分享一些相关的内容。我正在使用 java 8 和 Netbeans 8.1,当我创建 javafx FXML 应用程序时,我得到了这个。
以下是一些提示:

  1. 当您创建新项目时清理并构建您的项目,然后再尝试运行。
  2. 如果您重命名任何文件(控制器、fxml),IDE 不要将更改应用到其他文件,至少 Netbeans 不会这样做。因此,您必须手动更改其他文件中的这些文件名。
  3. 您可以在fxml 文件或main 类中定义控制器。 如果要在main 类中定义控制器,请使用@James_D 描述的方法。如果你想在fxml 文件中定义而不是使用fx:controller 属性作为

     fx:controller="yourProjectName.yourFXMLDocumentControllerName"
    

    并在主类中将其引用为

    Parent root = FXMLLoader.load(getClass().getResource("yourFXMLFileName.fxml"));
    
  4. 如果您认为一切都正确,但仍然收到错误clean and build,请再次尝试运行您的项目。

希望对某人有所帮助。

【讨论】:

    【解决方案3】:

    你用过

    Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
    

    试试

    Pane mainPane = FXMLLoader.load(getClass().getResource("/main.fxml"));
    

    【讨论】:

    • 这不是问题中问题的解决方案。如果路径错误,错误消息将是Location not set,而不是问题中发布的消息。
    【解决方案4】:

    我也经常遇到这种情况...... Scenebuilder/netbeans 8 中似乎有一个错误,在 Scenebuilder 中保存后,它会创建另一个 xmlns:fx="..." 所以这可能是一个问题.. .

    另外,看看我使用 java8 的 fxml,我有这些: xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"

    我注意到你的是: xmlns:fx="http://javafx.com/fxml"

    【讨论】:

    • 是的,当我更改该行但没有 java 8 时,它似乎没有太大变化,它只是使用 xmlns:fx="javafx.com/fxml/1"
    • 那将是一个完全不同的问题。如果是这种情况,xml 解析器会抱怨,这会导致堆栈跟踪看起来不同。 Caused by: java.lang.NoSuchMethodException: com.rpg.gui.MainController.&lt;init&gt;() 告诉您 FXMLLoader 正在尝试查找不可用的构造函数。
    【解决方案5】:

    在运行中>>编辑配置

    将此行添加到 VM 选项:

    --module-path /path/to/JavaFX/lib --add-modules=javafx.controls,javafx.fxml
    

    经过多次搜索后,这对我有用

    【讨论】:

    • 问题的作者使用 Java 8。Java 8 中是否已有模块?
    【解决方案6】:

    如果任何人没有通过以下答案解决问题,简单的方法是创建新的 fxml 空类,然后单击导致问题的 fxml 的 编辑复制 xml 代码 到新班级。 转到 start() 方法,更改:

    Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
    

    Pane mainPane = FXMLLoader.load(getClass().getResource("newClass.fxml"));
    

    【讨论】:

      【解决方案7】:
      layout.getChildren().addAll(choiceBox, button);
      

      我试图在布局中添加一个按钮,但我没有定义一个按钮对象,它看起来像这样:

      Button button;
      

      当我这样做时:

      button = new Button("Click me");
      

      一切开始正常。

      【讨论】:

        猜你喜欢
        • 2019-05-29
        • 1970-01-01
        • 2012-11-21
        • 2021-01-20
        • 2022-12-15
        • 2017-09-11
        • 1970-01-01
        • 2018-12-20
        • 1970-01-01
        相关资源
        最近更新 更多