提问者:小点点

JavaFX从FXML文件编辑Gridpane


我想通过java将标签添加到我的gridpane;因此,我使用了以下代码:

public class Controller {   
@FXML

private Button addTeam;
private GridPane teams;

public void addTeam(ActionEvent e) throws Exception {
        if(e.getSource()==addTeam) {
            Label apple = new Label("hello");
            teams.setConstraints(apple, 0, 0);
            teams.getChildren().add(apple);
        }
}

当我运行代码时,我得到一个包含在InvocationTargetException中的NullPointerException。

这里的null到底是什么?根据我目前的理解,“@FXML”标签实例化了我在其下方声明的变量。

堆栈:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java
.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown So
urce)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unkn
own Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(U
nknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.event.Event.fireEvent(Unknown Source)
        at javafx.scene.Node.fireEvent(Unknown Source)
        at javafx.scene.control.Button.fire(Unknown Source)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Un
known Source)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown S
ource)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown S
ource)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.h
andleBubblingEvent(Unknown Source)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unkn
own Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknow
n Source)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(U
nknown Source)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Sourc
e)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Sou
rce)
        at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
        at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
        at javafx.event.Event.fireEvent(Unknown Source)
        at javafx.scene.Scene$MouseHandler.process(Unknown Source)
        at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
        at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(Unknown Source)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEve
nt$355(Unknown Source)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown
 Source)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unkn
own Source)
        at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
        at com.sun.glass.ui.View.notifyMouse(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.Trampoline.invoke(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
        ... 61 more
Caused by: java.lang.NullPointerException
        at GuiController.addTeam(GuiController.java:42)
        ... 70 more

共1个答案

匿名用户

确保您从FXML文件中引用的每个变量在您的代码中都有@FXML注释。@FXML标记消除了实例化变量的需要,本质上,它使用fx: id查找变量。