При смене сцены fxml - все поля null
Создаю приложение на java с несколькими окнами. При загрузке основного окна прилоения - все поля в нем null. Сцены меняю с помощью класса:
public class ScreenController {
private final HashMap<String, Pane> screenMap = new HashMap<>();
private final Scene main;
public ScreenController(Scene main) {
this.main = main;
}
public void addScreen(String name, Pane pane){
screenMap.put(name, pane);
}
protected void removeScreen(String name){
screenMap.remove(name);
}
public void activate(String name){
main.setRoot( screenMap.get(name) );
}
public HashMap<String, Pane> getScreenMap() {
return screenMap;
}
}
Сеть в моем приложении синглтон, ее код:
@Slf4j
public class Controller implements Initializable {
// *** FXML *** //
// register
public TextField firstName;
public TextField lastName;
public TextField registerLogin;
public TextField email;
public TextField showRegisterPasswordField;
public PasswordField registerPassword;
public TextField showRegisterConfirmField;
public PasswordField registerConfirmField;
// login
public Button cancelButton;
public Button authButton;
public PasswordField passwordField;
public TextField loginField;
// app
public ListView <String> clientView;
public ListView <String> serverView;
public ProgressBar progressBar;
public Button sendFileButton;
public Button downloadFileButton;
public Button openClientDirectoryButton;
public Button openServerDirectoryButton;
public Label clientPathLabel;
public Label serverPathLabel;
// *** END FXML *** //
private static final App RUN = new App();
private static final ClientConfiguration CONFIG = new ClientConfiguration();
private static final EventHandler EVENT_HANDLER = new EventHandler();
private ObjectDecoderInputStream in;
private ObjectEncoderOutputStream out;
@Override
public void initialize(URL location, ResourceBundle resources) {
try {
Socket socket = new Socket(CONFIG.HOST, CONFIG.PORT);
out = new ObjectEncoderOutputStream(socket.getOutputStream());
in = new ObjectDecoderInputStream(socket.getInputStream());
refreshClientView();
Thread daemon = new Thread(() -> {
try {
while (true) {
Command command = (Command) in.readObject();
log.debug("received: {}", command);
switch (command.getType()) {
case AUTH_RESPONSE:
AuthResponseMessage authResponseMessage = (AuthResponseMessage) command;
EVENT_HANDLER.handleAuthResponse(authResponseMessage);
break;
case REGISTER_RESPONSE:
RegisterResponseMessage registerResponseMessage = (RegisterResponseMessage) command;
EVENT_HANDLER.handleRegisterResponse(registerResponseMessage);
break;
}
}
} catch (Exception e) {
log.error("exception while read from input stream", e);
}
});
daemon.setDaemon(true);
daemon.start();
} catch (IOException ioException) {
log.error("Error: ", ioException);
}
}
EVENT_HANDLER.handleRegisterResponse вызывает метод:
@SneakyThrows
public void setPathToClientDir(String rootDir) {
Platform.runLater(() -> {
RUN.toggleSceneToApp();
clientPathLabel.setText(rootDir);
});
}
В этом методе на строке
clientPathLabel.setText(rootDir);
возникает NullpointerException, тк clientPathLabel = null
Fxml код основного окна:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="514.0" prefWidth="573.0" xmlns="http://javafx.com/javafx/8.0.171"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="handlers.Controller">
<children>
<Label layoutX="35.0" layoutY="26.0" text="My Computer" />
<ListView fx:id="clientView" layoutX="35.0" layoutY="81.0" prefHeight="332.0" prefWidth="229.0" />
<Label layoutX="312.0" layoutY="26.0" text="Server" />
<ListView fx:id="serverView" layoutX="312.0" layoutY="81.0" prefHeight="332.0" prefWidth="229.0" />
<ProgressBar fx:id="progressBar" layoutX="28.0" layoutY="461.0" prefHeight="20.0" prefWidth="512.0"
progress="0.0" />
<Label layoutX="35.0" layoutY="431.0" text="File transfer:" />
<Button fx:id="sendFileButton" disable="true" layoutX="269.0" layoutY="204.0" mnemonicParsing="false"
prefHeight="26.0" prefWidth="19.0">
<font>
<Font size="15.0" />
</font>
<graphic>
<ImageView fitHeight="18.0" fitWidth="18.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="images/arrowRight.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="downloadFileButton" disable="true" layoutX="269.0" layoutY="243.0" mnemonicParsing="false"
prefHeight="26.0" prefWidth="19.0">
<font>
<Font size="15.0" />
</font>
<graphic>
<ImageView fitHeight="18.0" fitWidth="18.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="images/arrowLeft.png" />
</image>
</ImageView>
</graphic>
</Button>
<Button fx:id="openClientDirectoryButton" layoutX="211.0" layoutY="21.0" mnemonicParsing="false"
text="Open"/>
<Button fx:id="openServerDirectoryButton" layoutX="487.0" layoutY="21.0" mnemonicParsing="false"/>
<Label fx:id="clientPathLabel" layoutX="35.0" layoutY="45.0" text="Path:" />
<Label fx:id="serverPathLabel" layoutX="312.0" layoutY="45.0" text="Path:" />
</children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="184.0" prefWidth="203.0" xmlns="http://javafx.com/javafx/8.0.202-ea"
xmlns:fx="http://javafx.com/fxml" fx:controller="handlers.Controller">
<children>
<Button fx:id="cancelButton" layoutX="14.0" layoutY="149.0" mnemonicParsing="false"
onAction="#clickCancelButton" prefWidth="81.0"
text="Cancel" />
<Button fx:id="authButton" layoutX="108.0" layoutY="149.0" mnemonicParsing="false"
onAction="#sendAuthMessageToServer" prefWidth="81.0"
text="Login" />
<Label layoutX="14.0" layoutY="22.0" text="Login:" />
<PasswordField fx:id="passwordField" layoutX="8.0" layoutY="101.0" prefHeight="26.0" prefWidth="163.0" />
<TextField fx:id="loginField" layoutX="8.0" layoutY="45.0" prefWidth="187.0" />
<Label layoutX="14.0" layoutY="81.0" text="Password:" />
<CheckBox layoutX="177.0" layoutY="105.0" mnemonicParsing="false" />
</children>
</AnchorPane>