当前位置: 首页 > 知识库问答 >
问题:

JavaFX抛出错误“JavaFX Application Thread”[重复]

乐正涵忍
2023-03-14

我有个问题-

我需要换个场景。我在主文件中拥有的阶段和根场景:

package sample;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(Objects.requireNonNull(
                getClass().getResource("scenses/sample.fxml")));
        stage.setTitle("BlogApp");
        stage.setScene(new Scene(root, 600, 400));
        stage.show();
    }

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

同样我有控制器,有登录按钮。只有(找到用户)才会登录,并且场景必须更改为主场景:

button_auth.setOnAction(event -> {
    login_auth.setStyle("-fx-border-color: #fafafa");
    pass_auth.setStyle("-fx-border-color: #fafafa");
    button_reg.setText("Sign Up");

    if (login_auth.getCharacters().length() < 3) {
        login_reg.setStyle("-fx-border-color: red");
        return;
    } else if (pass_auth.getCharacters().length() < 3) {
        pass_auth.setStyle("-fx-border-color: red");
        return;
    }

    String pass = md5String(pass_auth.getCharacters().toString());
    try {
        boolean isAuth = db.authUser(login_auth.getCharacters().toString(), pass);
        if (isAuth) {
            login_auth.setText("");
            pass_auth.setText("");
            button_auth.setText("Logged in");
            Parent root = FXMLLoader.load(Objects.requireNonNull(
                    getClass().getResource("scenses/secondScene.fxml")));
            Scene homepage_scene = new Scene(root);
            Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
            window.setScene(homepage_scene);
            window.show();
        } else {
            button_auth.setText("Login is denied");
        }
    } catch (SQLException | ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
});

这给了我一个错误:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:221)
at sample.controllers.regController.lambda$initialize$1(regController.java:114)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8889)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3856)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1851)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2584)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

错误线114为

 Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("scenses/secondScene.fxml")));

我刚来Java,所以我真不明白为什么会这样。不管怎么说都是求救。

共有1个答案

麹权
2023-03-14

其实我不太明白你想做什么。但您给出了错误的getResource路径。这就是为什么你不能得到你想要达到的目标而得到一个NullPointerExcepiton的原因。你能更新如下吗?你能写作为一个评论,如果它是有效的吗?

Parent root = FXMLLoader.load(Objects.requireNonNull(
                getClass().getResource("sample.fxml")));
 类似资料:
  • 我有个问题- 我需要换个场景。我在主文件中拥有的阶段和根场景: 同样我有控制器,有登录按钮。只有(找到用户)才会登录,并且场景必须更改为主场景: 这给了我一个错误: 错误线114为 我刚来Java,所以我真不明白为什么会这样。不管怎么说都是求救。

  • 我最近刚开始使用kotlin进行android开发,所以在android studio也是一个彻头彻尾的傻瓜。我正在尝试构建一个超级简单的HelloWorld应用程序,但出现了以下错误: 现在必须显式声明注释处理器。发现编译类路径上的以下依赖项包含注释处理器。请将它们添加到annotationProcessor配置中。-jetified-kotlin-compiler-embeddeble-1.3

  • 我已经使用JPA工具创建了实体类,它已经为没有主键的表创建了两个类。一个是@Entity类,另一个是@嵌入式类,下面是两个类的代码 乡村语言 **CountrylanguagePK** 执行时会抛出错误。。 org.springframework.beans.factory.BeanCreationExctive:创建名为entityManagerFactory的bean时出错,该bean在类路径

  • 第一次安装熊猫时,网络出现故障并显示一些错误。当我再次尝试时,我得到以下错误。我用pip安装的任何其他东西都成功了,除了熊猫。

  • 当我尝试使用JQuery POST向服务器发送数据时,我得到以下错误。 我做错了什么? 未捕获的TypeError:在e处非法调用(https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js:4:7727)在Ab(https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.

  • 我已经安装了用于PHP开发的XAMPPv3.2.1。由于我已经安装了Skype并使用端口80和443,我将XAMPP配置为在端口1333(http.conf)和3333(https.conf)上运行。 我启动了xampp MySQL服务并成功启动。 当我导航到超文本传输协议://: 1333 xampp工作正常,但是如果我点击phpmyadmin我得到以下错误。 MySQL说:文档 服务器没有响应