应用程序启动方法异常 java.lang.reflect.InvocationTargetException
Exception in Application start method java.lang.reflect.InvocationTargetException
我刚开始使用 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
<?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
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();
}
}
我尝试了在 Whosebug 上找到的多个答案,但我找到的所有答案都是 2 年前的,对我的代码没有任何积极影响。
编辑:此处的堆栈跟踪:
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
猜猜是这个?
@FXML
void handleButton(ActionEvent event) {
未设置位置。
此异常表明代码无法访问您的 FXML 文件。
如果您的 fxml 文件位于任何包中,则使用包名称指定它。
如 James_D 和其他答案贡献者所提到的那样,对于将来遇到完全相同问题的任何人,删除路径开头的“/”可以解决问题,因此请使用
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
而不是
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
即使路径完全正确,也会出现此问题。
当您在更新的IDE[中创建 fxml 文件时=40=].
然后使用较旧的JavaFX Scene Builder来设计它.
解法:
在 JavaFX Scane Builder 中创建 fxml 文件
Design JavaFX Scane Builder 中的 fxml 文件,然后将其复制到 IDE 或项目.
我也遇到过这种情况,请大家关注我希望能成功:工具>选项>Java>JavaFX>Scenne Buider Home>更改Link。
你去 Scenne Buider 的路线不合适,我花了几个小时才找到,这很耗时,但在启动 JavaFX 时它总是很重要的。
执行以下操作
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
Parent root = loader.load();
删除.fxml 文件名前面的/。
我认为它应该工作。
它为我解决了同样的错误。
我在 Scene Builder 8.5 中工作时遇到了与 CORRECT PATH 相同的问题,我将其升级到 11.0,现在可以正常工作(我重新设计了所有 GUI)。
另一个原因可能是缺少 VM 选项:
--module-path <path-to-javafx>/javafx-sdk-11.0.2/lib
--add-modules=javafx.controls,javafx.fxml
我也遇到了同样的问题。这是因为maven库。我在场景生成器 jfoenix:0.1.8 和 IDE jfoenix:9.0.9.so 中有不同版本的 jfoenix 库,我在两者中下载了相同的版本,它对我有用。
还有空指针异常,这段代码对我有用:
FXMLLoader fxmlLoader=new FXMLLoader (Main.class.getResource ("sample.fxml"));
Parent root =fxmlLoader.load ();
fxml
文件和 Main
在同一个包中。
我一直在查看这个话题和答案,但情况对我来说有点不同
主文件中写的代码不是这样的:
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
我的代码是这样的:
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
我遇到了同样的错误(大约)
.getResource("MainWindowView.fxml"));和 .getResource(fxml + ".fxml"));
是已知会引发 NullPointer 和其他文件未找到异常的问题,即使文件路径完全有效,通过使用文件 Class 与 return 相同 [=17] 来摆脱它=] 值如前所述,您的代码应该可以正常工作。
回复评论是完全错误的.. – kleopatra
对你来说可能是这样,但是在应用程序启动方法 java.lang.reflect.InvocationTargetException 中有相同的异常,在 JavaFx8 版本下使用 .getResource(fxml + ".fxml") 作为触发器,这解决了我的问题并且它是解决方案,所以您的评论完全错误是针对您个人的,而不是一般性的。对于某些人来说,这正是他们需要的解决方案。
将.getResource()
替换为文件Class,returns相同URL,问题解决。只要文件路径有效,就无需担心将 / 添加到文件路径或使用相对或绝对文件路径 - 检查 f.exist 以确保。在遵循此平台上所有可能的线程答案后,我发布了我尝试并解决的问题,但没有有效结果。下面的这段代码为我解决了这个问题,所以如果它对你来说是完全错误的,就像之前评论的那样-kleopatra,那没关系。不适合我。
try {
File f = New File("");URL myfile = new File(f.getAbsolutePath()).toURL(); scene.getStylesheets().add(myfile.toExternalForm());
} catch(Exception e) {
}
解决方案:
1:通过以下命令将模块路径和模块添加到您的 VM 选项:
--module-path <path of your fx library> --add-modules javafx.contros,jafafx.fxml
应该可以通过上面的方法解决,如果没有解决则下一步
2: 创建一个单独的 fxml
并将其添加到您的项目中,它将起作用。
我刚开始使用 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
<?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
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();
}
}
我尝试了在 Whosebug 上找到的多个答案,但我找到的所有答案都是 2 年前的,对我的代码没有任何积极影响。
编辑:此处的堆栈跟踪:
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
猜猜是这个?
@FXML
void handleButton(ActionEvent event) {
未设置位置。
此异常表明代码无法访问您的 FXML 文件。
如果您的 fxml 文件位于任何包中,则使用包名称指定它。
如 James_D 和其他答案贡献者所提到的那样,对于将来遇到完全相同问题的任何人,删除路径开头的“/”可以解决问题,因此请使用
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
而不是
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
即使路径完全正确,也会出现此问题。
当您在更新的IDE[中创建 fxml 文件时=40=].
然后使用较旧的JavaFX Scene Builder来设计它.
解法:
在 JavaFX Scane Builder 中创建 fxml 文件
Design JavaFX Scane Builder 中的 fxml 文件,然后将其复制到 IDE 或项目.
我也遇到过这种情况,请大家关注我希望能成功:工具>选项>Java>JavaFX>Scenne Buider Home>更改Link。 你去 Scenne Buider 的路线不合适,我花了几个小时才找到,这很耗时,但在启动 JavaFX 时它总是很重要的。
执行以下操作
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
Parent root = loader.load();
删除.fxml 文件名前面的/。 我认为它应该工作。 它为我解决了同样的错误。
我在 Scene Builder 8.5 中工作时遇到了与 CORRECT PATH 相同的问题,我将其升级到 11.0,现在可以正常工作(我重新设计了所有 GUI)。
另一个原因可能是缺少 VM 选项:
--module-path <path-to-javafx>/javafx-sdk-11.0.2/lib
--add-modules=javafx.controls,javafx.fxml
我也遇到了同样的问题。这是因为maven库。我在场景生成器 jfoenix:0.1.8 和 IDE jfoenix:9.0.9.so 中有不同版本的 jfoenix 库,我在两者中下载了相同的版本,它对我有用。
还有空指针异常,这段代码对我有用:
FXMLLoader fxmlLoader=new FXMLLoader (Main.class.getResource ("sample.fxml"));
Parent root =fxmlLoader.load ();
fxml
文件和 Main
在同一个包中。
我一直在查看这个话题和答案,但情况对我来说有点不同
主文件中写的代码不是这样的:
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
我的代码是这样的:
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
我遇到了同样的错误(大约)
.getResource("MainWindowView.fxml"));和 .getResource(fxml + ".fxml"));
是已知会引发 NullPointer 和其他文件未找到异常的问题,即使文件路径完全有效,通过使用文件 Class 与 return 相同 [=17] 来摆脱它=] 值如前所述,您的代码应该可以正常工作。
回复评论是完全错误的.. – kleopatra 对你来说可能是这样,但是在应用程序启动方法 java.lang.reflect.InvocationTargetException 中有相同的异常,在 JavaFx8 版本下使用 .getResource(fxml + ".fxml") 作为触发器,这解决了我的问题并且它是解决方案,所以您的评论完全错误是针对您个人的,而不是一般性的。对于某些人来说,这正是他们需要的解决方案。
将.getResource()
替换为文件Class,returns相同URL,问题解决。只要文件路径有效,就无需担心将 / 添加到文件路径或使用相对或绝对文件路径 - 检查 f.exist 以确保。在遵循此平台上所有可能的线程答案后,我发布了我尝试并解决的问题,但没有有效结果。下面的这段代码为我解决了这个问题,所以如果它对你来说是完全错误的,就像之前评论的那样-kleopatra,那没关系。不适合我。
try {
File f = New File("");URL myfile = new File(f.getAbsolutePath()).toURL(); scene.getStylesheets().add(myfile.toExternalForm());
} catch(Exception e) {
}
解决方案:
1:通过以下命令将模块路径和模块添加到您的 VM 选项:
--module-path <path of your fx library> --add-modules javafx.contros,jafafx.fxml
应该可以通过上面的方法解决,如果没有解决则下一步
2: 创建一个单独的 fxml
并将其添加到您的项目中,它将起作用。