1

Ok, so I made my layout and I'm trying to get 2 labels, progressbar and 2 textFields.

I get them like this:

@FXML private TextField instDir;
@FXML private TextField jsonDir;
@FXML private ProgressBar progressBar;
@FXML private Label pText;
@FXML private Label error;

But only ones that aren't null are instDir and jsonDir. My FXML file: https://hastebin.com/cohuhidobi.xml Parts of Java class where I'm using objects:

void setProgressBar(float i, float max) {
    if(progressBar != null) {
        progressBar.setProgress(i / max);
    }else{
        System.out.println("progressBar is null");
    }
    if(pText != null) {
        pText.setText((int) i + "/" + (int) max);
    }else{
        System.out.println("pText is null");
    }
    System.out.println((int) i + "/" + (int) max);
    //text = (int)i+"/"+(int)max;
}

Which always returns both as null.

All of the Objects I want are registered in the controller

Thanks for the help!

3
  • 1
    at what moment do you call this method? You need to call FXMLLoader.load() already to have them initialized. Commented Jul 6, 2017 at 9:43
  • 1
    When do you call the setProgressBar method? If you call this in the constructor the @FXML gui stuff is not yet initialized. Use initialize method instead. Commented Jul 6, 2017 at 9:48
  • @SergeyGrinev I'm calling it after FXMLLoader.load(); Commented Jul 6, 2017 at 10:54

1 Answer 1

2

Check if your controller implements Initializable and has this structure:

public class FXMLController implements Initializable {

    @FXML private TextField instDir;
    @FXML private TextField jsonDir;
    @FXML private ProgressBar progressBar;
    @FXML private Label pText;
    @FXML private Label error;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // ...
    }

    //Getters and setters
}
Sign up to request clarification or add additional context in comments.

2 Comments

What url does initialize method get? I'm running it as a standalone app
The initialize method is called automatically when you create an instance of the controller. You don't need to set the url. See Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.