尝试重新放置场景内容时JavaFx中的指针异常

时间:2014-04-02 10:00:38

标签: java nullpointerexception null javafx javafx-2

我刚刚启动Java并使用Fx获取一些GUI,但在尝试替换我的scenecontent时我得到一个空指针异常。 在主要的加载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();
        }



}

Logincontroller类:

    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();

非常感谢提前

1 个答案:

答案 0 :(得分:0)

加载FXML后立即调用initialize()方法。看看here

initialize()之前调用setApp(Main application)。此时,application对象尚未初始化且为null

这意味着在{/ p>调用initialize()方法

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

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

profile.setApp(this);

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