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!

MG lolenstine
  • 919
  • 1
  • 11
  • 32
  • 1
    at what moment do you call this method? You need to call `FXMLLoader.load()` already to have them initialized. – Sergey Grinev Jul 06 '17 at 09: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](https://stackoverflow.com/a/34785689/2298490) method instead. – grill2010 Jul 06 '17 at 09:48
  • @SergeyGrinev I'm calling it after FXMLLoader.load(); – MG lolenstine Jul 06 '17 at 10:54

1 Answers1

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
}
Darkros
  • 251
  • 2
  • 11
  • What url does initialize method get? I'm running it as a standalone app – MG lolenstine Jul 06 '17 at 10:57
  • 1
    The initialize method is called automatically when you create an instance of the controller. You don't need to set the url. See [Documentation](https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/Initializable.html) – Darkros Jul 06 '17 at 11:15