Application start方法java.lang.reflect.InvocationTargetException中的异常

编程入门 行业动态 更新时间:2024-10-25 01:34:09
本文介绍了Application start方法java.lang.reflect.InvocationTargetException中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我刚刚开始使用JavaFX,我正在尝试使用标签,文本字段和按钮构建一个简单的应用程序,单击该按钮时,将标签的值设置为文本字段的值。一切顺利,直到我将控制器连接到主文件。这是我的代码:

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:

Main.java

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

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="javafx/javafx/8" xmlns:fx="javafx/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

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(); } }

我在StackOverflow上尝试了多个答案,但都是我发现它来自2年前,并没有对我的代码产生任何积极影响。

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.

编辑:此处的堆栈跟踪:

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$launchApplication$155(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$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) Exception running application application.Main

推荐答案

对于任何在将来遇到同样问题的人,如James_D和其他答案的贡献utors提到,删除路径开头的/修复问题所以使用

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

FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));

而不是

FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));

更多推荐

Application start方法java.lang.reflect.InvocationTargetException中的异常

本文发布于:2023-10-28 23:38:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   方法   start   Application   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!