I\'ve been trying button events between 2 scenes in Javafx.
This is Start.fxml (in view package):
OK so the NPE is being thrown on this line:
lbl1.setText(PreferencesController.txt2.getText());
At the basic Java level, are only two ways that you could get that to happen:
lnl1
is null
, orPreferencesController.txt2
is null
.No other explanations are possible ... given that PreferencesController.txt2
is a static field.
I'm not familiar with JavaFX or FXML, but I suspect that your mistake is declaring the fields as static
. I suspect that this causes the FXML mechanisms to not inject the reference to the UI component correctly. So those variables remain as null
... and you get an NPE.
Confirmation is here: https://stackoverflow.com/a/23109125/139985. (Thanks for the link @James_D)