运行 javafx 程序时的 InvocationTargetException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23132302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InvocationTargetException when running a javafx program
提问by Matthew Optional Meehan
So this worked in the example from javafx when My pc had jdk 1.7.0 so this may be the new version of FX in java8 however;
所以当我的电脑有 jdk 1.7.0 时,这在 javafx 的例子中起作用,所以这可能是 java8 中的新版本的 FX;
I get a nice stack-trace
我得到了一个很好的堆栈跟踪
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<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.rpg.gui.MainController">
0(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl.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.access0(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.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.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.access0(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication.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
basically telling me something is wrong here "com/rpg/gui/main.fxml:11"
基本上告诉我这里有问题 "com/rpg/gui/main.fxml:11"
and that line is
那条线是
MainController mainController = new MainController(path);
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
Window.setMainController(mainController);
Window.swap(path+"content.fxml");
so I can figure out that it doesn't like something to do with the main controller, so it has to be somthing to do with loading the fxml and the controller right? but thats about where my ideas end
所以我可以弄清楚它不喜欢与主控制器有关,所以它必须与加载 fxml 和控制器有关,对吗?但这就是我的想法结束的地方
FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
loader.setController(new MainController(path));
Pane mainPane = loader.load();
that's the method it throws everything in anyone experienced in javaFX or know of any changes in java8 that'd do this??
这就是它向任何有javaFX经验的人或知道java8中会这样做的任何变化的人抛出所有东西的方法?
采纳答案by James_D
Your MainController
doesn't have a zero-argument constructor. If the FXMLLoader
encounters a fx:controller
attribute on the root element, it attempts to create an instance of that controller by (effectively) calling the zero-argument constructor of the class specified in the attribute.
您MainController
没有零参数构造函数。如果在根元素上FXMLLoader
遇到fx:controller
属性,它会尝试通过(有效地)调用属性中指定的类的零参数构造函数来创建该控制器的实例。
To fix this (the simplest way), remove the fx:controller
attribute from the FXML file, and set the controller "by hand" on the FXMLLoader
. You need to create an FXMLLoader
instance instead of relying on the static load(...)
method:
要解决此问题(最简单的方法),请从 FXML 文件中删除该fx:controller
属性,并在 .xml 文件上“手动”设置控制器FXMLLoader
。您需要创建一个FXMLLoader
实例而不是依赖静态load(...)
方法:
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
回答by jdub1581
I also encounter that a lot... There seems to be a bug in Scenebuilder/netbeans 8, where upon saving in Scenebuilder, it creates another xmlns:fx="..." so that may be an issue...
我也遇到过很多... Scenebuilder/netbeans 8 中似乎有一个错误,在保存在 Scenebuilder 中时,它会创建另一个 xmlns:fx="..." 所以这可能是一个问题...
Also, looking at fxml's i have using java8, I have these: xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
另外,看看我使用 java8 的 fxml,我有这些: xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
where I noticed your's is: xmlns:fx="http://javafx.com/fxml"
我注意到你的是: xmlns:fx="http://javafx.com/fxml"
回答by idzik78
You used
你用过
Pane mainPane = FXMLLoader.load(getClass().getResource("/main.fxml"));
Try
尝试
fx:controller="yourProjectName.yourFXMLDocumentControllerName"
回答by Inzimam Tariq IT
I faced the same problem and want to share a little bit related to it. I'm using java 8 and Netbeans 8.1 and when I created a javafx FXML Application I got this one.
Here are some tips:
我遇到了同样的问题,想分享一些与之相关的内容。我正在使用 java 8 和 Netbeans 8.1,当我创建一个 javafx FXML 应用程序时,我得到了这个。
以下是一些提示:
- When you create new project clean and buildyour project before you try to run.
- If you rename any file (controller, fxml) IDE do not apply changes to other files at least Netbeans is not doing so. So, you have to change those file names in other files manually.
You can define controller either in
fxml
file or inmain
class. If you want to define controller inmain
class use the method described by @James_D. If you want to define infxml
file than usefx:controller
attribute asParent root = FXMLLoader.load(getClass().getResource("yourFXMLFileName.fxml"));
and in main class reference it as
fx:controller="yourProjectName.yourFXMLDocumentControllerName"
- If you think everything is correct but you still getting the error clean and buildyour project again and try to run.
- 当您创建新项目时,请在尝试运行之前清理并构建您的项目。
- 如果您重命名任何文件(控制器、fxml)IDE 不会将更改应用于其他文件,至少 Netbeans 不会这样做。因此,您必须手动更改其他文件中的这些文件名。
您可以在
fxml
文件或main
类中定义控制器。如果要在main
类中定义控制器,请使用@James_D描述的方法。如果你想在fxml
文件中定义而不是使用fx:controller
属性作为Parent root = FXMLLoader.load(getClass().getResource("yourFXMLFileName.fxml"));
并在主类中将其引用为
Pane mainPane = FXMLLoader.load(getClass().getResource("main.fxml"));
- 如果您认为一切都正确,但您仍然清除错误并再次构建您的项目并尝试运行。
Hope it would help someone.
希望它会帮助某人。
回答by Abderrahmane
If Any One does not fix the problem by following answers, The simple way is to create new fxml Empty class then click on editof the fxml causin the problem and copy the xml codeto the new class . Go to the start() methode, change the:
如果任何人都没有通过以下答案解决问题,简单的方法是创建新的 fxml Empty 类,然后单击引起问题的 fxml编辑并将 xml 代码复制到新类。转到 start() 方法,更改:
Pane mainPane = FXMLLoader.load(getClass().getResource("newClass.fxml"));
to
到
--module-path /path/to/JavaFX/lib --add-modules=javafx.controls,javafx.fxml
回答by parsa2820
In Run >> Edit Configurations
在运行中>>编辑配置
Add this line to VM Options:
将此行添加到 VM 选项:
##代码##this worked for me after tones of search
经过一番搜索之后,这对我有用