【问题标题】:Pointer exception in JavaFx when trying to recplace scene content尝试替换场景内容时 JavaFx 中的指针异常
【发布时间】:2014-05-13 12:37:12
【问题描述】:

我刚刚启动 Java 并使用 Fx 获得了一些 GUI,但在尝试替换我的场景内容时出现空指针异常。 在主要加载 Login.fxml 时没有任何问题,但是当我单击切换到另一个 fxml 的按钮时出现错误。

主要:

    package application;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.ResourceBundle;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXMLLoader;
    import javafx.fxml.Initializable;
    import javafx.fxml.JavaFXBuilderFactory;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;


public class Main extends Application {

    private Stage stage;

    @Override
    public void start(Stage primaryStage) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.sizeToScene();
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }

    private void gotoMainView() {
        try {
            MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
            profile.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void Userlogin()
    {
        gotoMainView();
    }
    private void gotoLogin() {
        try {
            LoginController login = (LoginController) replaceSceneContent("Login.fxml");
            login.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

     private Initializable replaceSceneContent(String fxml) throws Exception {
            FXMLLoader loader = new FXMLLoader();
            InputStream in = Main.class.getResourceAsStream(fxml);
            loader.setBuilderFactory(new JavaFXBuilderFactory());
            loader.setLocation(Main.class.getResource(fxml));
            AnchorPane page;
            try {
                page = (AnchorPane) loader.load(in);
            } finally {
                in.close();
            } 
            Scene scene = new Scene(page, 800, 600);
            stage.setScene(scene);
            stage.sizeToScene();
            return (Initializable) loader.getController();
        }



}

登录控制器类:

    package application;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.AnchorPane;

public class LoginController extends AnchorPane implements Initializable {
    @FXML
    static private Button btntext;
    @FXML
    static private TextField loginusr;
    @FXML
    static private PasswordField loginpwd;

    private Main application;

    public void setApp(Main application){
        this.application = application;
    }


     @Override  
     public void initialize(URL location, ResourceBundle resources) {       
        btntext.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
                System.out.println(loginusr.getText());
                System.out.println(loginpwd.getText());
                application.Userlogin();        
                }
        });     
    }

错误发生在:

application.Userlogin();

在此先感谢

【问题讨论】:

    标签: java nullpointerexception null javafx javafx-2


    【解决方案1】:

    一旦加载 FXML,就会调用 initialize() 方法。看看here

    您的initialize()setApp(Main application) 之前被调用。此时application对象还没有初始化,是null

    这意味着initialize()方法被调用在

    MainViewController profile = (MainViewController) replaceSceneContent("MainView.fxml");
    

    initialize() 内部使用的application 对象在此行之后实例化,当您使用时

    profile.setApp(this);
    

    因此,当您在initalize() 中尝试代码application.Userlogin(); 时,在执行时,application is null

    【讨论】:

    • 我真的很抱歉,但我不明白你的意思。你能解释一下吗?
    • 我已经编辑了我的答案,希望现在有意义!
    • 我理解这个问题,但解决这个问题的最佳方法是什么?我得到的样本只有一个场景,我在网上找到的其他场景确实不同
    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2016-02-15
    • 1970-01-01
    • 2014-12-13
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多