2

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>
Community
  • 1
  • 1
likethesky
  • 828
  • 3
  • 12
  • 28

2 Answers2

3

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.

jewelsea
  • 130,119
  • 12
  • 333
  • 365
  • Thanks jewelsea. I am wanting to serialize most properties of my stage/dialog, so I can keep them around for subsequent runs of the application. I figured the simplest way to do this (I'm not worried about performance) would be to 'peel off' only the properties (from the controller) into a separate object, then just serialize it. Following on your suggestion, I suppose I should leave all the properties as FXML properties on the original Controller. In that case, the auto-population of those properties can happen in the initialize of the Controller, when it'll deserialize and populate 'em. – likethesky Sep 18 '12 at 00:56
  • If you achieve to do it, please let me know how you serialized the javafx.beans.property object. I don't believe it's possible – Bruno Vieira Sep 18 '12 at 01:09
  • @jewelsea I would like to use the label that's already been created by my FXML file (I added it above). If I use your answer 2., the label is usable, but it doesn't appear on my form (of course). Is there a way to access the fx:id 'label' and change its text field (from 'Hey!' to something else, like 'Hello World!' in my example)? – likethesky Sep 18 '12 at 01:27
  • In the initialize method write `foo.label.setText("Hello World!");`, or you can use a resource bundle as described in the [fxml documentation](http://docs.oracle.com/javafx/2/api/javafx/fxml/doc-files/introduction_to_fxml.html). – jewelsea Sep 18 '12 at 02:37
  • I think you have answered my question, which is my *mistaken* belief that I can "use a label that's already been created by my FXML file," ... This can't be done. IOW, a 'deserialize' method can't simply write back into those fields/properties. I would have to write a custom deserialize (or deexternalize) that did `myFxmlField.setText(text read from InputStream);` on each property. At least, I think so! Thanks again jewelsea. – likethesky Sep 18 '12 at 18:02
  • Finally, just for future readers, here are a couple of other questions which I believe are related to mine--I had found them before writing my question, but didn't really understand them--I think now I do, a little bit better. Most relevant (for me, relating to the ability to update data in an FXML control) is: http://stackoverflow.com/questions/10751271/accessing-fxml-controller-class and perhaps also relevant (at least to jewelsea's answer 3. above): http://stackoverflow.com/questions/12166786/multiple-fxml-with-controllers-share-object HTHs someone! – likethesky Sep 18 '12 at 18:23
1

Man, you mixed the whole thing up! But it happens.

First, in example 1:

public String label;

is a String and not a Label. And I'm pretty sure you didn't use a String in your FXML file (Not that you can not, but it simply wouldn't be displayed in the Stage)

Anyway, when you use the "=" sign you are initializing your String, when you put the @FXML in a class which does not implement the Initializable interface you are not initializing anything from your FXML file (which by the way, must reference an Initializable as it's controller) and thus, you are referencing a null object.

To fix your problem in example 2 you need to:

// Example 2:

import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class SampleController implements Initializable {
    @FXML public Label label;

    @FXML private 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) {
    }    
}

and make sure you have a FXML file like:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Button?>

<AnchorPane xmlns:fx="http://javafx.com/fxml" fx:id="whatever-id" fx:controller="samplecontroller.package.SampleController">
   <children>
      <Label fx:id="label" text="not really necessary" />
      <Button fx:id"btnLabelChanger" onmouseclick="#handleButtonAction"/>
   </children>
</AnchorPane>

I hope it helped. Cheers.

Bruno Vieira
  • 3,794
  • 1
  • 21
  • 35
  • Appreciate the thoughts, Bruno. But no, the whole point of my question is when there's a *separate* class (called Foo in my example), how can I access the Label field? I *can* access it when it's a String (as shown in Example 1), but I can't when it's a Label. I think you hinted at my problem when you say, "when you put the @FXML in a class which does not implement the Initializable interface you are not initializing anything from your FXML file," but I believe that jewelsea has an answer that leads me closer to what I need. My comment on their answer describes why I want a separate class. – likethesky Sep 18 '12 at 01:05
  • Well, your question was: Can anyone explain why foo.label is null in the second example below? – Bruno Vieira Sep 18 '12 at 01:08
  • True, I did ask that. I also clarified the question to say *in a separate class* at the top and I also added my FXML file too. Thanks. Upvote for explaining the null part! – likethesky Sep 18 '12 at 01:18