0

I'm trying to create an address book using JavaFX. So far I've created the main window, I've created the ObeservableList that contains all of my person data, and a method to return said list since it's private. I have all of the contacts in the list being displayed correctly with along with their information in the primaryStage. When I click "new" to add a new contact, and I fill out their information in a second window then click the confirm/okay button I get some errors.

Here is my Main Class: (I apologize for posting so much code)

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

Here is my NewPersonController class:

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

And finally here is the console output after selecting the confirm button in the new contact window:

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

Thanks to everyone who reads this post, I know it's a lot but I've been working on this for over 2 days now and I've gotten nowhere. Any ideas are greatly appreciated, thanks again.

  • Which line is throwing the exception? – James_D Nov 17 '16 at 18:48
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fabian Nov 17 '16 at 19:03
  • main.getPersonData().add(person); this should be line 27 Caused by: java.lang.NullPointerException at application.NewPersonController.handleConfirm(NewPersonController.java:27). What kind of Node should main be? – SedJ601 Nov 17 '16 at 19:15
  • Why have you complicated this by adding all the stuff in the main class? – SedJ601 Nov 17 '16 at 19:21
  • This might be similar to what you are trying to do. http://code.makery.ch/library/javafx-8-tutorial/ – SedJ601 Nov 17 '16 at 19:30

1 Answers1

0

This is your problem. What are you trying to do here?

main.getPersonData().add(person); 

Your programming structure is confusing. Instead of creating a new controller and view to add new users. I would use a Javafx Dialog that opens when a button(btnAddNewUser) is pressed.

SedJ601
  • 12,173
  • 3
  • 41
  • 59