JavaFX-InvocationTargetException异常
JavaFX-InvocationTargetException
我是 JavaFX 和编程的新手,我遇到了无法 solve.I 尝试创建保存登录名和密码的应用程序的问题。我已经为它们完成了 MainApp 2 fxml 文件和控制器:
主应用程序:
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.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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$null8(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
请帮忙
您永远不会调用 ShowAccPaneController
的 setMainApp
方法,因此在 ShowAccPaneController
...
@FXML
private void handleOk(){
mainApp.showHomePane();
}
...mainApp
为空。
解决方案: 在 MainApp
的 showShowAccPane(Account acc)
方法中调用 controller.setMainApp(this);
(就像你在另一个控制器上所做的那样)。
我是 JavaFX 和编程的新手,我遇到了无法 solve.I 尝试创建保存登录名和密码的应用程序的问题。我已经为它们完成了 MainApp 2 fxml 文件和控制器:
主应用程序:
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.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.access00(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$handleMouseEvent4(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$null8(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
请帮忙
您永远不会调用 ShowAccPaneController
的 setMainApp
方法,因此在 ShowAccPaneController
...
@FXML
private void handleOk(){
mainApp.showHomePane();
}
...mainApp
为空。
解决方案: 在 MainApp
的 showShowAccPane(Account acc)
方法中调用 controller.setMainApp(this);
(就像你在另一个控制器上所做的那样)。