JavaFX FileChooser引发错误(可能很容易修复,但仍然困惑) [英] JavaFX FileChooser Throws Error (probably easy fix, but still confused)
问题描述
我正在尝试使用以下代码打开FileChooser:
I'm trying to open a FileChooser with this code:
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并显示以下错误消息:
but it is getting screwed up within the openFile() method. The compiler is throwing a NullPointerException with these error messages:
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。
Any help would be greatly appreciated, as I'm brand new to JavaFX and the use of Scene Builder.
推荐答案
这是我在两天内看到的第三个问题是人们重新初始化的字段@ FXML
- 由 FXMLLoader
注入。
This is the third question I've seen in two days that's been caused by people re-initializing fields that have been @FXML
-injected by the FXMLLoader
.
@FXML
是你告诉 FXMLLoader
使用通过解析FXML文件创建的对象初始化该字段。当你执行
The point of @FXML
is that you tell the FXMLLoader
to initialize that field with the object that was created by parsing the FXML file. When you then execute
btnBrowse = new Button();
创建一个新按钮,并且不再引用在解析FXML时为您创建的按钮。
in your initialize()
method, you create a new button and no longer have a reference to the one that was created for you when the FXML was parsed.
所以,现在应该很明显 btnBrowse.getScene()
将返回 null
(因为该按钮从未添加到场景中)和 btnBrowse.getScene()。getWindow()
将生成 NullPointerException
。
So, now it should be pretty obvious that btnBrowse.getScene()
will return null
(because the button was never added to a scene) and btnBrowse.getScene().getWindow()
will generate a NullPointerException
.
只需从 initialize()
方法中取出这三行。
Just take those three lines out of your initialize()
method.
我可以问你在哪里看到重新初始化那些字段吗?如果有一些流行的资源正在传播错误的代码,则需要将其删除。
Can I ask where you saw to re-initialize those fields like that? If there's some popular resource out there that's spreading incorrect code, it needs to be taken down.
这篇关于JavaFX FileChooser引发错误(可能很容易修复,但仍然困惑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!