1

First time asking here, and I'm pretty new to javafx and FXML with SceneBuilder. I am trying to change a label's text to what a user has entered in a textfield when a button is pushed, but I am getting errors. When I set the button's "on-action-method" to only print some random words it works.

Main:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {

        launch(args);
    }
}

Controller:

public class Controller {

    @FXML private TextField critTF;
    @FXML private Label label4;

    @FXML
    private void calculateButtonClicked (ActionEvent event){

        label4.setText(critTF.getText());
    }   
}

fx:id's are set to critTF and label4 in the FXML file and the fx:controller is set to sample.Controller on the root element which is the correct path.

Any help appreciated, thanks!

EDIT

FXML:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="476.0" prefWidth="376.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <GridPane layoutX="44.0" layoutY="138.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="85.0" minHeight="10.0" prefHeight="38.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="160.0" minHeight="10.0" prefHeight="142.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Haste">
               <padding>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </padding>
            </Label>
            <Label text="Critical Strike" GridPane.rowIndex="1">
               <GridPane.margin>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </GridPane.margin>
            </Label>
            <Label text="Mastery" GridPane.rowIndex="2">
               <GridPane.margin>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </GridPane.margin>
            </Label>
            <Label text="Versatility" GridPane.rowIndex="3">
               <GridPane.margin>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </GridPane.margin>
            </Label>
            <TextField id="hasteTF" GridPane.columnIndex="1">
               <GridPane.margin>
                  <Insets right="20.0" />
               </GridPane.margin>
            </TextField>
            <TextField id="critTF" GridPane.columnIndex="1" GridPane.rowIndex="1">
               <GridPane.margin>
                  <Insets right="20.0" />
               </GridPane.margin>
            </TextField>
            <TextField id="masteryTF" GridPane.columnIndex="1" GridPane.rowIndex="2">
               <GridPane.margin>
                  <Insets right="20.0" />
               </GridPane.margin>
            </TextField>
            <TextField id="versaTF" GridPane.columnIndex="1" GridPane.rowIndex="3">
               <GridPane.margin>
                  <Insets right="20.0" />
               </GridPane.margin>
            </TextField>
            <Button mnemonicParsing="false" onAction="#calculateButtonClicked" prefHeight="81.0" prefWidth="151.0" text="Calculate" GridPane.rowIndex="4">
               <GridPane.margin>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </GridPane.margin>
            </Button>
            <GridPane GridPane.columnIndex="1" GridPane.rowIndex="4">
              <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
              </columnConstraints>
              <rowConstraints>
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
              </rowConstraints>
               <children>
                  <Label text="Label" />
                  <Label text="Label" GridPane.rowIndex="1" />
                  <Label text="Label" GridPane.rowIndex="2" />
                  <Label id="label4" text="Label" GridPane.columnIndex="1" />
                  <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                  <Label text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
               </children>
            </GridPane>
         </children>
      </GridPane>
      <MenuBar layoutX="82.0" layoutY="2.0" prefHeight="25.0" prefWidth="128.0" AnchorPane.bottomAnchor="451.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <menus>
          <Menu mnemonicParsing="false" text="About">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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:8413)
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:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
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)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 48 more
Caused by: java.lang.NullPointerException
at sample.Controller.calculateButtonClicked(Controller.java:21)
... 58 more
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Post the FXML file, and the complete stack trace, in your question. – James_D Nov 13 '17 at 03:00
  • Couldn't get it to work before, sorry.. Is what I edited in sufficient? – Alvin H. Gusfre Nov 13 '17 at 13:38
  • The problem is that you haven't set any `fx:id` attributes, which is why you get the null pointer exception. For reference, instructions on formatting code are [here](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks). – James_D Nov 13 '17 at 13:42
  • Ok, I didn't realize that the id chosen in SceneBuilder was id and not fx:id. Thank you very much for helping out! :) – Alvin H. Gusfre Nov 13 '17 at 13:48
  • The `fx:id` attribute is set under the "Code" panel in the bottom right of Scene Builder. – James_D Nov 13 '17 at 13:49

0 Answers0