JavaFX setText()NullPointerException即使我已经设置了controller和fxml标签

时间:2018-05-25 17:34:54

标签: java javafx null fxml

当我切换到它时,我正在尝试将数据填充到我的新场景中。用户登录(这是有效的,因为如果我toString()他我得到了我需要的一切)。例如,当我在JFXTextField上调用setText()时,它不起作用。

这是我的控制器。当我切换到这个场景时调用Updatehome

public class DashboardController {

    @FXML JFXTextField txtGastDashboardSuchfeld;
    @FXML Label lblGastDashboardNachnameVorname;
    @FXML JFXTextField txtGastHomeVorname;
    @FXML JFXTextField txtGastHomeNachname;
    @FXML JFXTextField txtGastHomeSteuernummer;
    @FXML JFXTextField txtGastHomeAusweisnummer;
    @FXML JFXTextField txtGastHomeEmail;
    @FXML JFXPasswordField pwfGastHomePasswort;


    public void updateHome() {
        if(Main.user == null) {
            Controller.errorMessage("Fataler Fehler", "User konnte nicht aufgelöst werden!");
            return;
        }
        System.out.println(Main.user.toString());

        lblGastDashboardNachnameVorname.setText(Main.user.getVollername());
        txtGastHomeVorname.setText(Main.user.getVorname());
        txtGastHomeNachname.setText(Main.user.getNachname());
        txtGastHomeSteuernummer.setText(Main.user.getSteuernummer());
        txtGastHomeAusweisnummer.setText(Main.user.getAusweisnummer());
        txtGastHomeEmail.setText(Main.user.getEmailAdresse());
        pwfGastHomePasswort.setText(Main.user.getPasswort().toString());
    }

}

在我的主课程中我有这个(正在加载场景)

public static Controller controller;
public static DashboardController dashboardController;

public static Person user = null;

public static Stage window;

public static TreeMap<String, Scene> scenes = new TreeMap<>();
public static ArrayList<Scene> history = new ArrayList<>();

@Override
public void start(Stage primaryStage) {     
    window = primaryStage;
    controller = new Controller();
    dashboardController = new DashboardController();
    loadScenes();
}

这是将场景加载到我的树形图中的方法的片段

FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath));
        try {
            if(sceneName.contains("dashboard")) {
                loader.setController(dashboardController);
            } else
                loader.setController(controller);

            Parent parent = (Parent)loader.load();

            scenes.put(sceneName, new Scene(parent));

0 个答案:

没有答案