我在与两个控制器通信时遇到问题。
其中之一是MainPanelController,第二个是AddUserController。
在AddUserController中单击UserNameOkButton之后,我想在mainControl.getTopPanelController()。getUserSplitMenuButton()中创建新项目。
但是我在这方面有错误。

package simplygoals.control;
import simplygoals.model.*;
import simplygoals.control.AddUserController;
import java.util.ArrayList;
import javafx.scene.control.MenuItem;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import simplygoals.model.MySQLAccess;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.SplitMenuButton;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class MainPanelController extends MainController implements Initializable {


    @FXML
    private AddUserController addUserController;

    @FXML
    private TopPanelController topPanelController;

    @FXML
    private MenuPanelController menuPanelController;

    @FXML
    private LeftPanelTimeModeController leftPanelTimeModeController;

    @FXML
    private CenterPanelTableController centerPanelTableController;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //if there is no database create it, otherwise not

        getAppModel().getMySQL().createDatabase();
        getAppModel().copyUsersfromMySqlToAllUsers();

        createNewUserTopPanel();
        showUsersTopPanel();



    }
    public void showUsersTopPanel(){

        SplitMenuButton splitMenuButton = getTopPanelController().getUserSplitMenuButton();


                         for(String t:getAppModel().getAllUsers().getUsersString()){

                             final MenuItem menuItem = new MenuItem(t);
                             menuItem.setText(t);
                             splitMenuButton.getItems().add(menuItem);

                         }

    }
    public void createNewUserTopPanel(){
        MenuItem menuItem = getTopPanelController().getUserSplitMenuItem();
        menuItem.setOnAction(new EventHandler<ActionEvent>(){

                 public void handle(ActionEvent event) {
                            try {
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/AddUser.fxml"));
                        Parent root1 = (Parent) fxmlLoader.load();
                        Stage stage = new Stage();
                        stage.setScene(new Scene(root1));
                        stage.show();
                } catch(Exception e) {
                   e.printStackTrace();
                  }
                 }
        });
    }
    public TopPanelController getTopPanelController() {
        return topPanelController;
    }
}


还有第二个控制器:

package simplygoals.control;
import java.net.URL;
import javafx.fxml.Initializable;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import simplygoals.control.MainPanelController;
public class AddUserController extends MainController implements Initializable {

    private MainPanelController mainControl;

    @FXML
    private Button UserNameOkButton;

    @FXML
    private AnchorPane AddUserAnchor;

    @FXML
    private TextField UserNameTextField;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        UserNameOkButton.setOnAction(x->{
            getAppModel().addNewUserToMySql(UserNameTextField.getText());
            getAppModel().copyUsersfromMySqlToAllUsers();
            SplitMenuButton splitMenuButton = mainControl.getTopPanelController().getUserSplitMenuButton();
            splitMenuButton.getItems().clear();
            for(String t:getAppModel().getAllUsers().getUsersString()){
                final MenuItem menuItem = new MenuItem(t);
                menuItem.setText(t);
                splitMenuButton.getItems().add(menuItem);
            }
        });

    }


AddUserController是新窗口中的控制器。那里我有一个文本框和确定按钮。单击此菜单后,我想在menusplitbutton中创建新项,但是我必须使用MainPanelController。
如何在不同的窗口中通信两个控制器?
感谢帮助。
这里有错误:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at simplygoals.control.AddUserController.lambda$0(AddUserController.java:30)
    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:380)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
    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(Unknown Source)

最佳答案

您尚未在任何地方初始化mainControl

setMainControl()中定义一个AddUserController方法以对其进行初始化,并在加载AddUser.fxml时调用它:

menuItem.setOnAction(event ->  {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/simplygoals/view/AddUser.fxml"));
        Parent root1 = (Parent) fxmlLoader.load();
        Stage stage = new Stage();
        stage.setScene(new Scene(root1));

        AddUserController addUserController = fxmlLoader.getController();
        addUserController.setMainControl(this);

        stage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
});

09-26 04:12