0

I have three classes: HelloApplication, HelloController and ExtraClass.

HelloApplication: (basic boilerplate)

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        URL fxml = getClass().getResource("hello-view.fxml");
        FXMLLoader loader = new FXMLLoader(fxml);
        Parent root = loader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
        ExtraClass extraClass = new
        extraClass.marketRate(100);
    }

    public static void main(String[] args) {
        launch(args);
    }

}

HelloController:

public class HelloController {
    public Button welcomeButton;
    
    @FXML
    private Label welcomeText;

    @FXML
    public void setText(String string) {
        welcomeText.setText(string);
    }
}

ExtraClass:

public class ExtraClass {
    public void marketRate(int currentPrice) {
        String s = String.valueOf(currentPrice \* 1.05);
        HelloController helloController = new HelloController();
        helloController.setText(s);
    }
}

Now I get an error saying that this.welcomeText is null.

I understand that if I make a new controller it isnt the right one, it doesnt have the fxml loaded.

How can I make it work?

I've tried passing a controller to the ExtraClass and it works, but I want an approach without that.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Mikołaj
  • 11
  • 2
  • 1
    I would be more appropriate for `ExtraClass` to generate events that the controller "observed" and made appreciate updates - think about it, why would `ExtraClass` be responsible for updating the UI in the first place? Creating another instance of `HelloController` would simply disconnect the `ExtraClass` from the UI anyway. Part of the job of `FXMLLoader` is to load the fxml and bind it to the controller, which you've simply curumvented – MadProgrammer Mar 26 '23 at 00:09
  • if i would like to notify my subscriber aka controller i think i would still need somehow to call this controller @MadProgrammer – Mikołaj Mar 26 '23 at 00:23
  • 1
    No, you wouldn’t - or, at least, not directly. You’d provide a mechanism for the interested party to register/subscribe for events and then through that mechanism, generate events. Your ExtraClass would not care who they are notifying - think of a button - it doesn’t care who it’s notifying, it just notifies registered observers – MadProgrammer Mar 26 '23 at 00:26
  • So, instead, your controller would register itself as an observer to the ExtraClass – MadProgrammer Mar 26 '23 at 00:28
  • The duplicate demonstrates how to implement MadProgrammer's suggestions. – jewelsea Mar 26 '23 at 05:42
  • The premise of the question is odd. "I tried passing a controller...but I want an approach without that." If you want to access a member function of an object from another object, you have to pass a reference to the first object to the second object. This is just basic programming and you can't avoid it. – DaveB Mar 26 '23 at 12:53

0 Answers0