JavaFX FileChooser 引发错误(可能很容易修复,但仍然很困惑)

Posted

技术标签:

【中文标题】JavaFX FileChooser 引发错误(可能很容易修复,但仍然很困惑)【英文标题】:JavaFX FileChooser Throws Error (probably easy fix, but still confused) 【发布时间】:2014-05-30 11:08:42 【问题描述】:

我正在尝试使用以下代码打开 FileChooser:

package back_end.controllers;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.FileChooser;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created by Quinn McHugh on 4/15/2014.
 */
public class StartScreen_Controller implements Initializable 

    @FXML
    private ListView lstRecents;
    @FXML
    private Button btnBrowse;
    @FXML
    private BorderPane borderPane;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources)
        lstRecents = new ListView();
        btnBrowse = new Button();
        borderPane = new BorderPane();
    

    public void openFile()
        /* Presents a file chooser but does nothing functionally.
        */
        FileChooser fc = new FileChooser();
        File f = fc.showOpenDialog(btnBrowse.getScene().getWindow());
    

但它在 openFile() 方法中被搞砸了。编译器抛出 NullPointerException 并带有以下错误消息:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1760)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1643)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8196)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:97)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:90)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3712)
    at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3440)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1663)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2455)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:326)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:251)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:360)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
    at com.sun.glass.ui.View.notifyMouse(View.java:898)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
    at java.lang.Thread.run(Thread.java:744)
Caused by: 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:483)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
    ... 47 more
Caused by: java.lang.NullPointerException
    at back_end.controllers.StartScreen_Controller.openFile(StartScreen_Controller.java:38)
    ... 57 more

任何帮助将不胜感激,因为我是 JavaFX 和 Scene Builder 使用的新手。

【问题讨论】:

我不认为您发布的代码与堆栈跟踪匹配,因为它在 StartScreen_Controller 的第 38 行抱怨 NullPointerException,并且只有 37 行代码。您是否在将代码发布到 *** 之前对其进行了修改? 顺便说一句,抛出错误消息的不是您的编译器。它是 Java 运行时。如果您正在获取堆栈跟踪,那么根据定义,您已经成功编译了您的代码。 是的,我删除了其中只有 cmets 的两行。我会把它们放回去。然后在运行时与编译器上找到答案。 接下来要弄清楚:btnBrowse 是 null,还是 btnBrowse.getScene() 是 null?如果 btnBrowse 为空,那么您的初始化方法似乎没有被调用。否则,您可能需要做一些事情来让 btnBrowse 填充其scene 【参考方案1】:

这是我在两天内看到的第三个问题,它是由人们重新初始化由FXMLLoader 注入的@FXML 字段引起的。

@FXML 的意义在于告诉FXMLLoader 使用通过解析 FXML 文件创建的对象来初始化该字段。然后执行时

btnBrowse = new Button();

在您的 initialize() 方法中,您创建了一个新按钮,并且不再引用在解析 FXML 时为您创建的按钮。

所以,现在应该很明显btnBrowse.getScene() 将返回null(因为按钮从未添加到场景中)并且btnBrowse.getScene().getWindow() 将生成NullPointerException

只需将这三行从您的 initialize() 方法中取出即可。

请问您在哪里看到像这样重新初始化这些字段?如果有一些流行的资源在传播不正确的代码,则需要将其删除。

【讨论】:

我正在学习 Youtube 上某个免费的 JavaFX 教程,我不愿发布其名称,因为我知道这个人靠付费课程谋生。假设他是英国人,在 youtube 上搜索“javafx 教程”时,他的视频排名前 5 位。

以上是关于JavaFX FileChooser 引发错误(可能很容易修复,但仍然很困惑)的主要内容,如果未能解决你的问题,请参考以下文章

javafx如何导入文件

Javafx 可检索的拖放错误

javafx 场景生成器 2.0 中的文件选择器在哪里?我找不到它

Java开发桌面程序学习——文件选择器和目录选择器的使用

使用 HOC 可拖动元素反应 DnD 引发无法设置未定义错误的属性“道具”

JavaFX 8 中的一般异常处理