JavaFX - Label & mdash; How to access the class member

advertisements

So my question is very simple. I can access class members in a separate class when they are a String rather than a Label, but I can't seem to, when they are Label. Using JDK 7u6 (w/ JavaFX 2.2).

Simple examples. First one works, second one doesn't. foo.label can be assigned in the first example, but I get a NullPointer on the second example. Can anyone explain why foo.label is null in the second example below?

UPDATE: I removed the @FXML annotations from my original question, because I didn't think they were necessary to the problem I was having. Also, see my comment on the answer by @jewelsea ... Finally, I have added my FXML file, for completeness (at bottom of Q).

This works:

// Example 1:
public class SampleController implements Initializable {
    Foo foo = new Foo();

    public void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        foo.label = "Hello World!";
        System.out.println(foo.label);
    }

    @Override public void initialize(URL url, ResourceBundle rb) {
    }
}

// Example 1 -- Foo.java:
public class Foo {
    public String label;
}

And this does not work:

// Example 2:
public class SampleController implements Initializable {
    Foo foo = new Foo();

    public void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        foo.label.setText("Hello World!");  // gives NullPointer exception !!
    }

    @Override public void initialize(URL url, ResourceBundle rb) {
    }
}

// Example 2 -- Foo.java:
import javafx.scene.control.Label;

public class Foo {
    public Label label;
}

Here is my FXML file, for either of the examples above:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication4.SampleController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" text="hey!" />
    </children>
</AnchorPane>


In your second example you are never initializing the foo label to anything, so it will remain null.

You do create a Foo object with Foo foo = new Foo(); but that isn't going to initialize the label field member inside of Foo. In particular, the @FXML annotation isn't going to do anything in the way in which you have utilized it in the code provided. This is because Foo is not a controller.

Some ways to allow your example to run:

1. Get rid of Foo

  • Move @FXML public Label label; into your SampleController.
  • Get rid of the Foo class and ensure that your fxml defines the fx:id="label".

2. Initialize the Foo label in your SampleController

  • Place the following code in your Sample Controller's initialize method: foo.label = new Label();.
  • Also, in initialize, add the foo label to some container like a layout pane so that it will be visible.

3. Make Foo a Nested Controller

  • Keep the Foo class.
  • Initialize the Foo instance in your controller using @FXML Foo foo; instead of Foo foo = new Foo();.
  • Make the Foo class a controller (implements Initializable or defines an initialize method with an appropriate signature).
  • In your fxml which references SampleController, also have an fx:include statement for a new foo.xml document which sets the fx:id="label" to allow the Foo instance to be initialized.

The third way seems closest to what you want and documentation for it is in the Nested Controllers section of the Introduction to FXML document.

Note: This question is more about how to initialize members of classes using @FXML then how to access the class members. Access is just through normal getters/setters or member field access as you already have in your example.

Your first example works because you are explicitly the initializing foo.label to a new string (using foo.label = "Hello World!";) before you use it.