我正在尝试使用JavaFX创建一个地址簿。到目前为止,我已经创建了主窗口,我已经创建了包含我所有人员数据的ObeservableList,以及一个返回所述列表的方法,因为它是私有的。我在列表中的所有联系人以及他们在primaryStage中的信息都正确显示。当我点击“新建”添加新联系人,并在第二个窗口中填写他们的信息,然后单击确认/确定按钮我收到一些错误。
这是我的主要课程:(我为发布这么多代码而道歉)
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
mainWindow();
}
public void mainWindow() {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
AnchorPane pane = loader.load();
Scene scene = new Scene(pane);
MainWindowController controller = loader.getController();
controller.setMain(this);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public void newPersonWindow() {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource("NewPersonView.fxml"));
AnchorPane pane = loader.load();
Scene scene = new Scene(pane);
Stage stage = new Stage();
NewPersonController controller = loader.getController();
controller.setMain(this, stage);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
private ObservableList<Person> personData = FXCollections.observableArrayList(); // plain list that contains all of our data *note it's private so we must create a method to call it
public ObservableList<Person> getPersonData() { // method that returns the private personData list
return personData;
}
public Main() { // create the constructor of the main class
//adds person
personData.add(new Person("Carson", "Clark", "250-415-6675", "Vancouver", "g8y-4d2", "god@live.ca")); // a new instance from the person model
personData.add(new Person("John", "Smith", "543-476-4188", "Victoria", "x8g-4a2", "wackerboo@hotmail.com"));
personData.add(new Person("Eric", "Ranch", "762-562-5477", "New York", "k8b4s4", "sk8r432@gmail.com"));
personData.add(new Person("Sam", "Smith", "425-475-6512", "Washington", "b8y-9d2", "birdhunter21@gmail.com"));
personData.add(new Person("Jake", "Carter", "462-888-1705", "Nanimo", "h8x-4c8", "skiguy327@hotmail.com"));
}
public static void main(String[] args) {
launch(args);
}
}
这是我的NewPersonController类:
package application;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class NewPersonController {
@FXML private TextField firstNameField, lastNameField, phoneField, cityField, postalField, emailField;
private Stage stage;
private Main main;
public void setMain(Main main, Stage stage) {
this.main = main;
this.stage = stage;
}
@FXML
public void handleConfirm() { // I think this is where the error is occuring but I'm not sure why
Person person = new Person(
firstNameField.getText(),
lastNameField.getText(),
phoneField.getText(),
cityField.getText(),
postalField.getText(),
emailField.getText()
);
main.getPersonData().add(person);
stage.close();
}
@FXML
public void handelCancel() {
stage.close();
}
}
最后,在新的联系人窗口中选择确认按钮后,这是控制台输出:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.NullPointerException
at application.NewPersonController.handleConfirm(NewPersonController.java:27)
... 58 more
感谢阅读这篇文章的所有人,我知道这很多,但我已经在这方面工作了2天以上而且我无处可去。非常感谢任何想法,再次感谢。
答案 0 :(得分:0)
这是你的问题。你想在这做什么?
main.getPersonData().add(person);
您的编程结构令人困惑。而不是创建新的控制器和视图来添加新用户。我会使用按下按钮(btnAddNewUser)时打开的Javafx对话框。