Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:32:07+00:00 2026-06-11T11:32:07+00:00

So my question is very simple. I can access class members in a separate

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:32:09+00:00Added an answer on June 11, 2026 at 11:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very simple question, but I can't seem to find something about
A very simple question I can't seem to find a definitive answer for. I
got a very simple question which I can't seem to find any answer for.
I have a very simple question for which I can't seem to find an
This is probably a very simple question so please forgive my ignorance, but can
I'm sure this is a very simple question, but embarrassed to say I can't
Maybe a very simple question. How can I put in this code <Query> <Where>
I have a very simple question, How can I get the POST values from
I'm hoping this question has a very simple answer. I can think of ways
Its very simple question, I'm new to this and I can't understand the problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.