传递给新窗口的JavaFX参数变为NULL [重复]

Posted

技术标签:

【中文标题】传递给新窗口的JavaFX参数变为NULL [重复]【英文标题】:JavaFX parameter passed to new window becomes NULL [duplicate] 【发布时间】:2019-10-11 21:53:42 【问题描述】:

我的问题是我传递给新创建的窗口的 NOT NULL 参数是 NULL。我已经阅读了很多关于如何通过设置器将参数传递到另一个窗口的帖子,对我来说似乎一切都很好,但它不起作用。

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



我在上面的代码中通过了Person。但在下面的代码中,它似乎为空。 myUser setter 在文件末尾-

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;
    

myUsermainMenu 似乎是 NULL,正如调试器所说。 错误信息:

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)

【问题讨论】:

@GeorgeZ。问题不会因为名称中有相似的文字而重复。 OP 显然已经知道 NPE 是由 null 变量引起的,并且知道错误的来源。这里的问题是为什么这个变量是null @Kröw 由 OP 调试他的代码。查看代码和堆栈跟踪,myUser 仍然是null,因为setMyUser 被调用之后 initialize 被调用(来自loader.load())。这些都是副本所涵盖的所有内容。 【参考方案1】:

您可以从堆栈跟踪中看到崩溃发生在mainMenu.userMenuController.showMyBooks() 方法(第一行at XXX)上,该方法由mainMenu.userMenuController.initialize()(第二行at XXX)调用。

因为JavaFX在获取控制器对象之前调用了initialize()方法,所以这个p成员变量当时显然没有初始化,也就是说它是一个空指针(成员变量初始化为null,说Java) .

有关调用顺序的更多详细信息,请参阅this question。

顺便说一下,一般建议类的第一个字符使用大写

【讨论】:

以上是关于传递给新窗口的JavaFX参数变为NULL [重复]的主要内容,如果未能解决你的问题,请参考以下文章

无法在android studio中的child()中为参数'pathString'传递null [重复]

Javafx怎么播放flash,就是我用Java main函数执行,直接调用Javafx做好的页面,为啥不能播放flash

如何将null值传递给简单注入器中的构造函数?

试图在intellij jre11上运行javafx程序[重复]

JavaFX - 建议在 setLayoutX(x) 上使用 relocate(x,y) 移动对象,但我无法修改重定位参数 [重复]

PyQt5按钮lambda变量变为布尔值[重复]