Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
来自FXML文件的JavaFX编辑网格窗格_Java_Javafx_Invocationtargetexception_Gridpane - Fatal编程技术网

来自FXML文件的JavaFX编辑网格窗格

来自FXML文件的JavaFX编辑网格窗格,java,javafx,invocationtargetexception,gridpane,Java,Javafx,Invocationtargetexception,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("he

我想通过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

这里什么是空的?根据我目前的理解,'@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

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

标签将变量初始化为读取FXML文件时由
fxmloader
创建的实例。因此,您确实需要显示您的FXML文件才能确定。如果您能指出您发布的代码中的哪一行是第42行,这将非常有帮助。说了这么多。。。由于在
teams
变量上没有
@FXML
注释,只在
addteams
变量上,所以
teams
很可能是
null
(第42行是指
teams
的第一行:
teams.setConstraints(…)
)。遗憾的是,我认为FXML标记只需要使用一次,但正如您所说的,我必须用FXML标记对每个变量进行注释。谢谢你的帮助!注释只是语句的一部分。注意
@FXML
和后续代码之间没有分号,因此整个语句是
@FXML private Button addTeam。下一个代码形成下一个语句:
privategridpane团队
(从技术上讲,注释是修饰符:
@FXML
在声明中的状态与关键字
private
相同)