javafx应用程序在尝试设置标签文本时返回null [重复]

时间:2021-06-06 17:01:49

This question already has an answer here:

这个问题在这里已有答案:

 clueless about this error..i am trying to set the text property of a label when i click on a button..it keeps giving this error  :




 Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
        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:8390)
        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:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        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:3758)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$136/128942111.get(Unknown Source)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:927)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
        at com.sun.glass.ui.win.WinApplication$$Lambda$38/636526176.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
    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:497)
        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:497)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
        ... 50 more
    Caused by: java.lang.NullPointerException
        at classes.LoginController.output(LoginController.java:26)
        ... 60 more

this is the controller:

这是控制器:

public class LoginController  {

    @FXML private Label labelOne;


    public void output(ActionEvent actionEvent) {
        labelOne.setText("HEY");
    }
}

this is the fxml code for the controls:

这是控件的fxml代码:

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="250.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8" 
      xmlns:fx="http://javafx.com/fxml/1" fx:controller="classes.LoginController">
   <children>
      <Label layoutX="43.0" layoutY="117.0" prefHeight="17.0" prefWidth="66.0" text="Username" />
      <TextField id="textField" layoutX="141.0" layoutY="113.0" prefHeight="25.0" prefWidth="169.0" />
      <Label layoutX="43.0" layoutY="165.0" prefHeight="17.0" prefWidth="66.0" text="Password" />
      <PasswordField id="passwordField" layoutX="141.0" layoutY="161.0" prefHeight="25.0" prefWidth="169.0" />
      <Label alignment="CENTER" contentDisplay="CENTER" layoutX="87.0" layoutY="33.0" prefHeight="47.0" prefWidth="180.0" text="LOGIN">
         <font>
            <Font size="31.0" />
         </font>
      </Label>
      <Button id="loginButton" layoutX="244.0" layoutY="211.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="66.0" text="Login" />
      <Button id="signupButton" layoutX="43.0" layoutY="211.0" mnemonicParsing="false" text="SignUp" onAction="#output" />
      <Label id="labelOne" layoutX="141.0" layoutY="79.0" prefHeight="30.0" prefWidth="125.0" >
         <font>
            <Font size="20.0" />
         </font>
      </Label>
   </children>
</Pane>

i am certain this is a fairly basic problem but i'm stuck and clueless on this problem..also what is the best method i could use to connect a javafx application to a database...and what database would be the best option to embed within this application.....

我确信这是一个相当基本的问题,但我对这个问题感到困惑和无能为力。另外,我可以用来将javafx应用程序连接到数据库的最佳方法是什么...以及什么数据库是最好的选择嵌入此应用程序.....

1 个解决方案

#1


1  

use H2 database which is best embedded and also on localhost and the second issue

使用最好嵌入的H2数据库,也可以使用localhost和第二个问题

add fx: to your ids

将fx:添加到你的ids

like

<Label fx:id="labelOne" layoutX="141.0" layoutY="79.0" prefHeight="30.0" prefWidth="125.0" >
         <font>
            <Font size="20.0" />
         </font>
      </Label>

#1


1  

use H2 database which is best embedded and also on localhost and the second issue

使用最好嵌入的H2数据库,也可以使用localhost和第二个问题

add fx: to your ids

将fx:添加到你的ids

like

<Label fx:id="labelOne" layoutX="141.0" layoutY="79.0" prefHeight="30.0" prefWidth="125.0" >
         <font>
            <Font size="20.0" />
         </font>
      </Label>