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.