上善若水含洪八埏 2024-05-30 12:49 采纳率: 33.3%
浏览 8

JavaFX使用Scene Builder启动报错

在JavaFX运行中无法启动
以下是报错

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    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.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
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:1583)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3369)
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3332)
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3300)
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3272)
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3248)
    at javafx.fxml@21.0.3/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3241)
    at JavaFxStudent.Main.changeView(Main.java:36)
    at JavaFxStudent.Main.start(Main.java:30)
    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:400)
    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 JavaFxStudent.Main

这个是文件图

img

这个是FXMl初始配置:fx:controller="JavaFxStudent.JavaFXImpl.Logins"
这个是FXMl文件的代码

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="JavaFxStudent.JavaFXImpl.Logins">
   <children>
      <Label layoutX="144.0" layoutY="116.0" prefHeight="101.0" prefWidth="271.0" text="Fuck" />
      <Button layoutX="187.0" layoutY="209.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="196.0" text="Buttons" />
   </children>
</AnchorPane>



这个是控制类的源代码

package JavaFxStudent.JavaFXImpl;

import javafx.fxml.FXML;

/**
 * @program: JavaFx
 * @ClassName Logins
 * @description(类信息): 登陆界面
 * @author: honor
 * @create: 2024-05-30 11:32
 * @Version 1.0
 **/
public class Logins {


}


这个是测试类源代码

package JavaFxStudent;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

/**
 * @program: JavaFx
 * @ClassName Main
 * @description(类信息): 测试类
 * @author: honor
 * @create: 2024-05-30 11:30
 * @Version 1.0
 **/
public class Main extends Application {
    private static Stage stage;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Main.stage=stage;
        stage.setTitle("Fuck");
        changeView("src/JavaFxStudent/JavaFxml/JavaFX.fxml");
        stage.show();
    }
    public static void changeView(String fxml){
        Parent root = null;
        try {
            root = FXMLLoader.load(Main.class.getResource(fxml));
            stage.setScene(new Scene(root));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}


这个是虚拟机选项配置
--module-path "D:\JavaFX21\javafx-sdk-21.0.3\lib" --add-modules javafx.controls,javafx.fxml

最开始使用17JDK无效,重新下了21JDK运行,重写了代码依旧不行,但是不使用fxml直接运行FX没有问题,怀疑是Fxml问题
二编:

在使用前面赤兔回答进行修缮后依旧报错,但是说的是Cound not find Fxml 即FXNML文件为空

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:118)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    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.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1135)
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:1583)
Caused by: java.lang.RuntimeException: Could not find FXML file:D:\文档\IDEA\JavaFx\src\JavaFxStudent\JavaFxml\JavaFX.fxml
    at JavaFxStudent.Main.changeView(Main.java:39)
    at JavaFxStudent.Main.start(Main.java:31)
    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:400)
    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 JavaFxStudent.Main

这是修改过后的FXML文件源代码


<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="JavaFxStudent.JavaFXImpl.Logins">
    <children>
        <Label layoutX="144.0" layoutY="116.0" prefHeight="101.0" prefWidth="271.0" text="Fuck"/>
        <Button layoutX="187.0" layoutY="209.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="196.0"
                text="Buttons" onAction="#handButtonClick"/>
    </children>
</AnchorPane>

这是修改过后的Login控制台源代码

package JavaFxStudent.JavaFXImpl;

import javafx.fxml.FXML;
import javafx.scene.control.Button;

import java.awt.event.ActionEvent;

/**
 * @program: JavaFx
 * @ClassName Logins
 * @description(类信息): 登陆界面
 * @author: honor
 * @create: 2024-05-30 11:32
 * @Version 1.0
 **/
public class Logins {

    @FXML
    private Button button;


    @FXML
    public void handButtonClick(javafx.event.ActionEvent actionEvent) {
        System.out.println("Button clicked!");
    }
}

这是测试类源代码

package JavaFxStudent;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

/**
 * @program: JavaFx
 * @ClassName Main
 * @description(类信息): 测试类
 * @author: honor
 * @create: 2024-05-30 11:30
 * @Version 1.0
 **/
public class Main extends Application {
    private static Stage stage;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Main.stage = stage;
        stage.setTitle("Fuck");
        changeView("D:\\文档\\IDEA\\JavaFx\\src\\JavaFxStudent\\JavaFxml\\JavaFX.fxml");
        stage.show();
    }

    public static void changeView(String fxml) {
        Parent root = null;
        try {
            URL reso = Main.class.getResource(fxml);
            if (reso == null) throw new RuntimeException("Could not find FXML file:" + fxml);
            root = FXMLLoader.load(reso);
            stage.setScene(new Scene(root));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}


  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-30 12:49
    关注

    引自免费微信小程序:皆我百晓生

    看起来你遇到的问题是由于FXML加载失败导致的NullPointerException。根据错误消息,问题出在FXMLLoader.load()方法上,它要求提供一个有效的文件位置。让我们一步步解决这个问题。

    首先,确认一下你的项目结构是否正确。确保JavaFX.fxml文件位于src/JavaFxStudent/JavaFxml/目录下,并且与Main.java在同一项目的根目录下(即JavaFxStudent)。如果你使用的是IntelliJ IDEA,确保项目设置正确,资源文件被包含在构建路径中。

    然后,检查changeView()方法中的FXML文件路径。这里有一个潜在的错误,因为getResource()方法可能不会找到相对于主类的相对路径。尝试将路径更改为绝对路径,例如:

    public static void changeView(String fxml){
        Parent root = null;
        try {
            // 使用类路径的URL,而不是相对路径
            URL resource = Main.class.getResource("/JavaFxStudent/JavaFxml/" + fxml);
            if (resource == null) {
                throw new RuntimeException("Could not find FXML file: " + fxml);
            }
            root = FXMLLoader.load(resource);
            stage.setScene(new Scene(root));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    

    接着,确保你的JavaFX环境配置正确。在命令行中使用以下参数启动应用:

    --module-path "D:\JavaFX21\javafx-sdk-21.0.3\lib" --add-modules javafx.controls,javafx.fxml
    

    这应该放在你的IDE的运行配置中,作为VM选项。确保你的JDK版本与JavaFX SDK版本兼容。在这种情况下,你使用了JDK 21,这是与JavaFX 21相匹配的。

    最后,虽然不是引起当前问题的原因,但为了遵循良好的编程实践,你应该在Logins类中添加一些实际的功能。至少为按钮添加一个事件处理器,这样可以验证FXML和控制器之间的连接是否正常:

    package JavaFxStudent.JavaFXImpl;
    
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    
    public class Logins {
    
        @FXML
        private Button buttons;
    
        @FXML
        private void handleButtonClick(ActionEvent event) {
            System.out.println("Button clicked!");
        }
    }
    

    接下来,更新你的FXML文件,给按钮添加onAction属性,以便将其绑定到控制器的方法:

    <Button layoutX="187.0" layoutY="209.0" mnemonicParsing="false" prefHeight="71.0" prefWidth="196.0" text="Buttons" onAction="#handleButtonClick" />
    

    现在,按照上述步骤修改代码后,重新编译并运行你的应用程序,看看是否解决了问题。如果有任何新的错误或问题,请告诉我。

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 5月30日
  • 创建了问题 5月30日

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵