MG lolenstine
MG lolenstine

Reputation: 969

JavaFX returning null when getting objects from FXML

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!

Upvotes: 1

Views: 976

Answers (1)

Darkros
Darkros

Reputation: 251

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
}

Upvotes: 2

Related Questions