运行javafx程序时InvocationTargetException

因此,这在我的电脑具有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中有任何变化的人会这样做吗?

回答:

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

要解决此问题(最简单的方法),请

,然后在上“手动”设置控制器FXMLLoader。您需要创建一个FXMLLoader实例,而不是依赖于静态load(...)方法:

FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));

loader.setController(new MainController(path));

Pane mainPane = loader.load();

以上是 运行javafx程序时InvocationTargetException 的全部内容, 来源链接: utcz.com/qa/419661.html

回到顶部