1

My problem is that the NOT NULL parameter which I passed to newly created window is NULL. I've read many posts about how to pass parameter to another window via setters and for me it seems like everything is fine, but it doesn't work.

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

}

I passed Person in above code. But in below code it seems to be null. myUser setter is at the end of the file-

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;
    }
}

myUser as well as mainMenu seems to be NULL as debugger says. ERROR MESSAGE:

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)
S.Plox
  • 11
  • 2
  • @GeorgeZ. Questions don't become duplicates just because they have similar text in the name. The OP clearly already knows that the NPE is being caused by a `null` variable, and knows the error's origin. The problem here is *why* that variable is `null`. – Kröw May 25 '19 at 18:23
  • 1
    @Kröw It is for the OP to debug his code. Looking at the code and the stacktrace, `myUser` is still `null` because `setMyUser` is called **after** `initialize` has been called (from `loader.load()`). Those are all things covered by the duplicate. – Mark Rotteveel May 26 '19 at 07:35

1 Answers1

0

You can see from the stack trace that the crash happens on mainMenu.userMenuController.showMyBooks() method (first at XXX line) which is called by mainMenu.userMenuController.initialize() (second at XXX line).

Because JavaFX calls the initialize() method before you get the controller object, this p member variable is clearly not initiallized at that time, which means it is a null pointer (member variables are initialized as null, said Java).

See this question for more detail about the calling sequence.

By the way, it's generally recommended to use upper case for the first character of a class.

renyuneyun
  • 608
  • 5
  • 18