我的问题是我传递给新创建的窗口的非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
设置器在文件的末尾 -
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
以及 mainMenu
似乎是无效的,如Debugger所说。错误消息:
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)
您可以从堆栈跟踪中看到崩溃发生在mainMenu.userMenuController.showMyBooks()
方法(第一个at XXX
行(上,该方法由mainMenu.userMenuController.initialize()
(第二at XXX
行(调用。
因为javafx在获取控制器对象之前调用initialize()
方法,因此该p
成员变量显然不是在当时初始启动,这意味着它是一个null指针(Member变量被初始化为null,Java表示,Java表示。
有关调用序列的更多详细信息,请参见此问题。
顺便说一句,通常建议使用上案例作为类的第一个字符。