Answer the question
In order to leave comments, you need to log in
How to pass Class1 variables to Class2 if in Class1 they are associated with FXML?
the .fxml file has a Text object with its own fx:id
<Text fx:id="system_messages" layoutX="202.0" layoutY="638.0" strokeType="OUTSIDE" strokeWidth="0.0" text="сообщений пока нет..." wrappingWidth="889.69921875">
public class MainController implements Initializable {
@FXML
Text system_messages;
...
public class SystemMessagesController {
MainController main = new MainController();
public void system_messages(String type) {
switch (type) {
case "UnknownHostException": {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
main = (MainController) fxmlLoader.getController ();
main.system_messages.setText("Ошибка получения данных. Проверьте подключение к интернету.");
}
default: {
main.system_messages.setText("Ошибка получения данных. Ошибка неизвестна.");
}
}
}
}
Caused by: java.lang.NullPointerException: Cannot read field "system_messages" because "this.main" is null
Answer the question
In order to leave comments, you need to log in
You are getting an error in the SystemMessagesController class with a value null
for the reason that yours system_messages
is in another class, namely MainController.
You need to pass system_messages
from the MainController class to the SystemMessagesController class! And in the class itself, get system_messages
and substitute in replacements for the text of the message. And also in yours main.fxml
should be
<Text fx:id="get_system_messages" layoutX="202.0" layoutY="638.0" strokeType="OUTSIDE" strokeWidth="0.0" text="сообщений пока нет..." wrappingWidth="889.69921875">
main.get_system_messages.setText("сюда передаём system_messages");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question