应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35282724/
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
Exception in Application start method java.lang.reflect.InvocationTargetException
提问by Robert Valencia
I am just starting out with JavaFX, and I am trying to build a simple application with a label, text field and button which, when clicked, sets the label's value to that of the text field's. Everything was going well until I connected the controller to the Main file. Here's my code:
我刚刚开始使用 JavaFX,我正在尝试构建一个带有标签、文本字段和按钮的简单应用程序,单击该按钮时,会将标签的值设置为文本字段的值。一切都很顺利,直到我将控制器连接到主文件。这是我的代码:
Main.java
主程序
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage; // connect primary stage
mainWindow();
}
// main window
public void mainWindow() {
try {
// view
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
AnchorPane pane = loader.load();
// controller
MainWindowController mainWindowController = loader.getController();
mainWindowController.setMain(this);
// scene on stage
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
MainWindowView.fxml
主窗口视图.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController">
<children>
<Label fx:id="label" alignment="CENTER" layoutX="291.0" layoutY="164.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<font>
<Font size="20.0" />
</font>
</Label>
<HBox alignment="CENTER" layoutX="201.0" layoutY="208.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<TextField fx:id="field" layoutX="201.0" layoutY="208.0" />
<Button layoutX="381.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButton" text="Change Text" />
</children>
</HBox>
</children>
</AnchorPane>
MainWindowController.java
主窗口控制器.java
package application;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class MainWindowController {
// views
@FXML private Label label;
@FXML private TextField field;
private Main main;
// connect main class to controller
public void setMain(Main main) {
this.main = main;
}
// assign text field text to label on button click
public void handleButton() {
String text = field.getText();
label.setText(text);
field.clear();
}
}
I have tried multiple answers found on StackOverflow, but all that I have found were from 2 years ago and did not make any positive effect on my code.
我尝试了在 StackOverflow 上找到的多个答案,但我发现的所有答案都是 2 年前的,并没有对我的代码产生任何积极影响。
EDIT: Stack trace here:
编辑:此处的堆栈跟踪:
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:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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:497)
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:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.Main.mainWindow(Main.java:27)
at application.Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application application.Main
采纳答案by Robert Valencia
For anyone who has this exact same problem in the future, as James_D and the other answer contributors mentioned, removing the "/" at the beginning of the path fixes the problem so use
对于将来遇到完全相同问题的任何人,正如 James_D 和其他答案贡献者所提到的,删除路径开头的“/”可以解决问题,因此请使用
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
instead of
代替
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
回答by Elltz
guess its this ?
猜猜是这个?
@FXML
void handleButton(ActionEvent event) {
回答by Prasanna Venkat
Location is not set.
This exception states that your FXML file is unreachable by the code.
If you have your fxml file inside any package then specify it with the package name.
未设置位置。
此异常表明代码无法访问您的 FXML 文件。如果您在任何包中都有 fxml 文件,请使用包名称指定它。
回答by Nour Noby
This problem can also occure even when the path is completely right.
即使路径完全正确,也会出现此问题。
When you createthe fxmlfile in an Updated IDE.
Then use an older JavaFX Scene Builderto designit.
当你创建了FXML在文件更新IDE。
然后使用较旧的 JavaFX Scene Builder来设计它。
Solution :
解决方案 :
Createthe fxmlfile in JavaFX Scane Builder
Designthe fxmlfile in JavaFX Scane Builder then copy this to the IDE or Project.
创建的FXMLJavaFX中SCANE生成器文件
设计的FXMLJavaFX中SCANE生成器文件,则该复制到IDE或项目。
回答by Duc Vo
I also met this case, you please follow me hope to succeed: Tool> Options> Java> JavaFX> Scenne Buider Home> Change Link. Your route to Scenne Buider is unsuitable, it took me hours to find, it is time consuming but it will be a matter of always important when starting JavaFX.
我也遇到过这种情况,希望成功的请关注我:Tool>Options>Java>JavaFX>Scenne Buider Home>Change Link。您到 Scenne Buider 的路线不合适,我花了几个小时才找到,这很耗时,但在启动 JavaFX 时总是很重要的问题。
回答by joshiparas
Perform the following operation
执行以下操作
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
Parent root = loader.load();
Remove / from in front of .fxml file name. I think it should work. It solved the same error for me.
删除 .fxml 文件名前面的 /。我认为它应该工作。它为我解决了同样的错误。
回答by Said Pc
i've had the same problem with the CORRECT PATH i was working in scene builder 8.5 i upgraded it to 11.0 and now its working (i've redesigned all the GUI).
我在使用场景构建器 8.5 时遇到了与正确路径相同的问题,我将其升级到 11.0,现在可以正常工作(我重新设计了所有 GUI)。
回答by user3460055
Another cause might be missing VM options:
另一个原因可能是缺少 VM 选项:
--module-path <path-to-javafx>/javafx-sdk-11.0.2/lib
--add-modules=javafx.controls,javafx.fxml
回答by Komal Samdariya
The same problem happened to me. It's because of maven library. I have different version of jfoenix library in scene builder jfoenix:0.1.8 and in IDE jfoenix:9.0.9.so i downloaded the same version in both and it worked for me.
同样的问题发生在我身上。这是因为 Maven 库。我在场景构建器 jfoenix:0.1.8 和 IDE jfoenix:9.0.9. 中有不同版本的 jfoenix 库,所以我在两者中下载了相同的版本,它对我有用。
And also for the nullpointer exception this code worked for me:
而且对于空指针异常,这段代码对我有用:
FXMLLoader fxmlLoader=new FXMLLoader (Main.class.getResource ("sample.fxml"));
Parent root =fxmlLoader.load ();
The fxml
file and Main
is in same package.
该fxml
文件Main
是在同一个包。