运行javafx程序时调用TargetException

运行javafx程序时调用TargetException,java,netbeans,javafx,Java,Netbeans,Javafx,这在javafx的例子中起了作用,当时我的pc上有JDK1.7.0,所以这可能是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 meth

这在javafx的例子中起了作用,当时我的pc上有JDK1.7.0,所以这可能是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
所以我可以看出它不喜欢与主控制器有关,所以它必须与加载fxml和控制器有关,对吗?但这就是我的想法的终点

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

这是一种方法,它向任何有过javaFX经验或知道java8有任何变化的人抛出所有东西???

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

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

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

我也遇到了很多。。。Scenebuilder/netbeans 8中似乎存在一个bug,在保存到Scenebuilder中时,它会创建另一个xmlns:fx=“…”,因此这可能是一个问题

另外,通过查看使用java8的fxml,我有以下几点: xmlns=”http://javafx.com/javafx/8“xmlns:fx=”http://javafx.com/fxml/1"

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

您使用

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


我也面临着同样的问题,我想和大家分享一些与此相关的信息。我正在使用java 8和Netbeans 8.1,当我创建一个JavaFXML应用程序时,我得到了这个应用程序。
以下是一些提示:

  • 创建新项目时,请在尝试运行之前清理并生成项目
  • 如果重命名任何文件(控制器、fxml),IDE不会将更改应用于其他文件,至少Netbeans不会这样做。因此,您必须手动更改其他文件中的文件名
  • 您可以在
    fxml
    文件或
    main
    类中定义控制器。 如果要在
    main
    类中定义控制器,请使用@James\u D描述的方法。如果要在
    fxml
    文件中定义,请使用
    fx:controller
    属性作为

     fx:controller="yourProjectName.yourFXMLDocumentControllerName"
    
    在主类中引用它作为

    Parent root = FXMLLoader.load(getClass().getResource("yourFXMLFileName.fxml"));
    
  • 如果您认为一切都是正确的,但仍然出现错误,请清理并重新构建项目并尝试运行

  • 希望它能帮助到其他人。

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

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


    在运行>>编辑配置中

    将此行添加到VM选项:

    --module-path /path/to/JavaFX/lib --add-modules=javafx.controls,javafx.fxml
    
    在搜索之后,这对我起了作用

    layout.getChildren().addAll(choiceBox, button);
    
    我试图在布局中添加一个按钮,但我没有定义一个按钮对象,它看起来是这样的:

    Button button;
    
    当我这样做的时候:

    button = new Button("Click me");
    

    一切都开始正常工作。

    我对JavaFX一无所知,但看看堆栈跟踪,它似乎希望
    MainController
    有一个无参数构造函数(它没有)。什么是
    com.rpg.gui.gui.loadMainPane(gui.java:34)
    ?显示代码dude@parsaporahmad我假设
    GUI.java:34
    Pane mainPane=fxmloader.load(getClass().getResource(“main.fxml”),现在包含在OP中。我去掉了构造函数,现在使用setPath(path),但它抱怨没有看到content.fxml的位置,我应该编辑上面的帖子吗?我的意思是,从技术上讲,它已经修复了这个问题。我不认为这与从JavaFX2.2更改为Java8有任何关系:您的代码也会失败。是的,当我更改这一行时,这似乎没有太大变化,但如果没有Java8,它只是使用xmlns:fx=“”,这将是一个完全不同的问题。如果是这种情况,xml解析器会抱怨,这将导致stacktrace看起来不同<代码>原因:java.lang.NoSuchMethodException:com.rpg.gui.MainController。(
    告诉您
    FXMLLoader
    正在尝试查找不可用的构造函数。这不是问题的解决方案。如果路径错误,错误消息将是
    Location not set
    ,而不是问题中发布的消息。上述代码中的
    FXMLLoadergetClass()
    是什么?我是外汇新手help@InzimamTariqIT修正了输入错误。好的,我的代码现在可以不用这个了。我刚刚清理并构建了项目,一切都很好。如果我们删除
    fx:controller
    属性并手动添加controller,我们需要手动添加所有操作,因为如果中的fxml文件中没有对controller的引用,则会显示更多按钮错误和其他我们在xml中定义操作的内容。@InzimamTariqIT否,你不应该那样做。它的作用方式与您在FXML中声明控制器的方式完全相同。
    layout.getChildren().addAll(choiceBox, button);
    
    Button button;
    
    button = new Button("Click me");