JavaFX的的InvocationTargetException

时间:2016-06-17 11:48:25

标签: java javafx invocationtargetexception

我是JavaFX和编程的新手,我遇到了无法解决的问题。我正在尝试执行保存登录名和密码的应用程序。我已经为他们完成了MainApp 2 fxml文件和控制器:

MainApp:

public class MainApp extends Application{

    private Stage primaryStage;
    private BorderPane rootLayout;
    private ObservableList<Account> accounts = FXCollections.observableArrayList();

    public ObservableList<Account> getAccounts() {
        return accounts;
    }

    public MainApp(){
        accounts.add(new Account("Google", "sdfgg", "dfbuifb"));
        accounts.add(new Account("Facebook", "fhnhfbf", "dsuyfaa87yisd"));
    }

    @Override
    public void start(Stage primaryStage){
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("AccApp");

        initRootLayout();
        showHomePane();
    }

    public void initRootLayout(){
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("/pl/kitron/account/view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();

            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);

            primaryStage.show();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public void showHomePane(){
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("/pl/kitron/account/view/HomePane.fxml"));
            AnchorPane homePane = (AnchorPane) loader.load();

            rootLayout.setCenter(homePane);

            HomePaneController controller = loader.getController();
            controller.setMainApp(this);

        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public void showShowAccPane(Account acc){
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainApp.class.getResource("/pl/kitron/account/view/ShowAccPane.fxml"));
            AnchorPane homePane = (AnchorPane) loader.load();

            rootLayout.setCenter(homePane);

            ShowAccPaneController controller = loader.getController();
            controller.showAccount(acc);
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }
}

HomePaneController:

public class HomePaneController implements Initializable {

    @FXML
    private TableColumn<Account, String> accountColumn;

    @FXML
    private TableView<Account> accountTable;

    private MainApp mainApp;

    public HomePaneController(){}

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        accountColumn.setCellValueFactory(cellData -> cellData.getValue().accountNameProperty());
    }    

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;

        accountTable.setItems(mainApp.getAccounts());
    }

    @FXML
    private void handleShowAccount(){
        Account selectedAccount = accountTable.getSelectionModel().getSelectedItem();
        if(selectedAccount != null){
            mainApp.showShowAccPane(selectedAccount);
        }else{
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.initOwner(mainApp.getPrimaryStage());
            alert.setTitle("Błąd");
            alert.setHeaderText("Nie wybrałeś konta");
            alert.setContentText("Wybierz konto z tabeli i spróbuj jeszcze raz!");

            alert.showAndWait();
        }
    }
}

ShowAccController:

public class ShowAccPaneController implements Initializable {

    @FXML
    private Label accountNameLabel;

    @FXML
    private Label accNameLabel;

    @FXML
    private Label loginLabel;

    @FXML
    private Label passwordLabel;

    private MainApp mainApp;

    public ShowAccPaneController(){}

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }

    public void setMainApp(MainApp mainApp) {
        this.mainApp = mainApp;
    }

    public void showAccount(Account account){
        accountNameLabel.setText(account.getAccountName());
        accNameLabel.setText(account.getAccountName());
        loginLabel.setText(account.getLogin());
        passwordLabel.setText(account.getPassword());
    }

    @FXML
    private void handleOk(){
        mainApp.showHomePane();
    }
}

我可以点击方法handleShowAccount的按钮,一切正常。但是,当我尝试使用方法handleOk点击按钮时,我有这样的错误:

Error: Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
 at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
 at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
 at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
 at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
 at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
 at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
 at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
 at javafx.event.Event.fireEvent(Event.java:198)
 at javafx.scene.Node.fireEvent(Node.java:8411)
 at javafx.scene.control.Button.fire(Button.java:185)
 at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
 at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
 at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
 at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
 at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
 at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
 at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
 at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
 at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
 at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
 at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
 at javafx.event.Event.fireEvent(Event.java:198)
 at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
 at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
 at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
 at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
 at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
 at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
 at java.security.AccessController.doPrivileged(Native Method)
 at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
 at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
 at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
 at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
 at com.sun.glass.ui.View.notifyMouse(View.java:937)
 at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
 at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:497)
 at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
 at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:497)
 at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
 at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771) ... 48 more
Caused by: java.lang.NullPointerException
 at pl.kitron.account.controller.ShowAccPaneController.handleOk(ShowAccPaneController.java:47) ... 58 more

请帮忙

1 个答案:

答案 0 :(得分:2)

您永远不会在setMainApp ... {/ p>中调用ShowAccPaneController ShowAccPaneController方法

@FXML
private void handleOk(){
    mainApp.showHomePane();
}

... mainApp为空。

解决方案showShowAccPane(Account acc) MainApp方法controller.setMainApp(this);(正如您在其他控制器上所做的那样)。