Przekazanie parametru do nowego okna i NullPointerException

0

Witam. Mój problem polega na tym, że gdy wywołuje nowe okno za pomocą JavaFX i przekazuje do jego klasy kontrolującej obiekt który chcę modyfikować, który nie jest NULL'em, to mimo wszystko zamienia się on w NULL'a i nie wiem co się dzieje. Dodam tylko że mam odpowiedni setter do ustawienia referencji.

Klasa mainMenuController przekazuje obiekt Person do userMenuController, a tam nie mogę nic z nim zrobić bo mam NullPointerException...

MainMenuController.java

public void loginPerson(ActionEvent e) {

try {

    Person p = look_up.accessUser(loginTabEmail.getText(), loginTabPassword.getText()); // ITS NOT NULLL

    if (p == null) {
        System.out.println("loginPerson: Bad login");
    } else {
        System.out.println("User logged in! " + loginTabEmail.getText() + " " + loginTabPassword.getText());
        loginTabEmail.clear();
        loginTabPassword.clear();

        FXMLLoader loader = new FXMLLoader(getClass().getResource("userMenu.fxml"));
        SplitPane newWindow = (SplitPane) loader.load();
        userMenuController controller = loader.getController();
        controller.setMainMenu(this);
        controller.setMyUser(p); // trying to pass Person to another controller
        Stage stage = new Stage();
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(registerTabSignUp.getScene().getWindow());
        stage.setTitle("MyLibrary");
        Scene scene = new Scene(newWindow);
        stage.setScene(scene);
        stage.show();
    }
} catch (Exception i) {
    System.err.println("Client exception: " + e.toString());
    i.printStackTrace();
}

}

userMenuController.java

public class userMenuController implements Initializable {

    private mainMenuController mainMenu;
    private Person myUser;
    private static RMIInterface look_up;

    private ObservableList<Book> userBooksObservableList = FXCollections.observableArrayList();
    private ObservableList<Book> libraryObservableList = FXCollections.observableArrayList();

    @FXML
    private ListView<Book> myBooksSection;

    @FXML
    private ListView<Book> libraryBooksSection;

    public userMenuController() throws Exception {

        look_up = (RMIInterface) Naming.lookup("//localhost/MyServer");
    }

    public void showMyBooks() {
        myBooksSection.setItems(userBooksObservableList);
        userBooksObservableList.addAll(myUser.getPersonBooks()); // myUser is NULL ?? why ??

        userBooksObservableList.add(new Book("sadas"));
    }

    public void showLibraryBooks() throws MalformedURLException, RemoteException, NotBoundException {

        libraryBooksSection.setItems(libraryObservableList);
        libraryObservableList.addAll(look_up.getAvailableBooks());
    }

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

        try {
            showMyBooks();
            showLibraryBooks();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setMainMenu(mainMenuController ctrl) {
        mainMenu = ctrl;
    }

    public void setMyUser(Person p) {
        myUser = p;
    }
}

Błąd:

java.lang.NullPointerException
    at mainMenu.userMenuController.showMyBooks(userMenuController.java:46)
    at mainMenu.userMenuController.initialize(userMenuController.java:61)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at mainMenu.mainMenuController.loginPerson(mainMenuController.java:66)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:835)
0

Metoda initialize jest wołana automatycznie, zaraz po konstruktorze zanim zdążysz ustawić myUser. Przenieś to co robisz w initialize do innej metody i zawołał ją ręcznie.

0

Ok udało mi się coś na to poradzić. Odnośnie problemu, cytat ze Stackoverflow: > In a few words: The constructor is called first, then any @FXML annotated fields are populated, then initialize() is called. So the constructor does NOT have access to @FXML fields referring to components defined in the .fxml file, while initialize() does have access to them.
Zatem na starcie obiekt myUser jest nullem, więc muszę jakoś opóźnić wykonanie metody showMyBooks. Zrobiłem to tak:
Platform.runLater(() -> {
userBooksListView.setItems(userBooksObservableList);
userBooksObservableList.addAll(myUser.getPersonBooks());
});
Jeżeli ktoś zna inne rozwiązanie to może się podzielić :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1