Eclipse 2024-03中JavaFX报Exception in Application start method错误求助
JavaFX FXML加载错误解决方法
问题概述
作为JavaFX新手,尝试多种方案并升级至Eclipse 2024-03(4.31.0)后,仍反复遇到FXML加载错误。已确认FXML文件路径正确,但启动程序时抛出GridPane is not a valid type异常。
代码展示
Main.java
package application; import javafx.application.*; import javafx.stage.*; import javafx.scene.*; import javafx.fxml.*; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { FXMLLoader loader = new FXMLLoader(Main.class.getResource("/Test.fxml")); if (loader.getLocation() == null) { System.out.println("Failed to load FXML"); return; } Parent root = loader.load(); stage.setScene(new Scene(root)); stage.setTitle("Account"); stage.sizeToScene(); stage.show(); } }
Test.fxml(原错误版本)
<?xml version="1.0" encoding="UTF-8"?> <?import java.net.*?> <?import javafx.geometry.*?> <?import java.scene.control.*?> <?import java.scene.layout.*?> <?import java.scene.text.*?> <GridPane xmlns:fx="http://javafx.com/fxml/1"> alignement="center" hgap="10" vgap="10" > <Label text="Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextFiels GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="Balance" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <TextFiels GridPane.columnIndex="1" GridPane.rowIndex="2"/> </GridPane>
错误信息
Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464) at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1085) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893) at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:840) Caused by: javafx.fxml.LoadException: GridPane is not a valid type. /C:/Users/edgar/eclipse-workspace-2024/BankAccount/bin/Test.fxml at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2718) at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2920) at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2850) at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2649) at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563) at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531) at application.Main.start(Main.java:20) at javafx.graphics@21.0.3/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839) at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483) at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics@21.0.3/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455) at javafx.graphics@21.0.3/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics@21.0.3/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185) ... 1 more Exception running application application.Main
解决步骤
- 修正GridPane的导入路径:FXML中错误导入了
java.scene.layout.*,但GridPane属于JavaFX的javafx.scene.layout包,需将导入语句改为:<?import javafx.scene.layout.*?> - 修正GridPane标签语法与拼写:将属性移至起始标签内部,同时修正
alignement的拼写错误为alignment,正确的GridPane标签写法:<GridPane xmlns:fx="http://javafx.com/fxml/1" alignment="center" hgap="10" vgap="10"> - 修正文本框组件拼写:将
TextFiels改为正确的TextField(缺失字母d)。
修正后的Test.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import java.net.*?> <?import javafx.geometry.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.text.*?> <GridPane xmlns:fx="http://javafx.com/fxml/1" alignment="center" hgap="10" vgap="10"> <Label text="Name:" GridPane.columnIndex="0" GridPane.rowIndex="1"/> <TextField GridPane.columnIndex="1" GridPane.rowIndex="1"/> <Label text="Balance" GridPane.columnIndex="0" GridPane.rowIndex="2"/> <TextField GridPane.columnIndex="1" GridPane.rowIndex="2"/> </GridPane>
内容的提问来源于stack exchange,提问作者Edgar Chauve
相关产品推荐
相关产品推荐

