0

I have de following Problem.

I have 2 FXML Controllers. 1. For Add Member 2. For Add Relation

Click on Add Relation Button from main program create instance od second controller.

After run program when I add New Member all works fine. When I click on Add Relation and next on button cancel or accept, and next I go to add new member and I do SUBMIT, the initialize method of second Controller is called. I dont understand why

@Override
public void initialize(URL url, ResourceBundle rb) {

    log.trace(LogMessages.MSG_CTRL_INITIALIZATION);

    this.languageBundle.setValue(rb);
    relationType.setItems(FXCollections.observableArrayList(
            RelationType.NEUTRAL,
            RelationType.LOVE,
            RelationType.FIANCE,
            RelationType.MARRIED
    ));



    /*
        COMBOBOX CELL FACTORY
     */

    setCellFactoryToCombobox(simLeftChoice);
    setCellFactoryToCombobox(simRightChoice);
    setCellFactoryToCombobox(childChoice);
    setCellFactoryToRelationType(relationType);
    toggleActiveButton.selectedProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue == true) {
            toggleActiveButton.setText("Active");
        } else {
            toggleActiveButton.setText("Inactive");
        }
    });

    toggleActiveButton.setSelected(true);

    addLanguageListener();
    addRegisterButtonDisableCondition();

    /*
        POPULATE COMBOBOX
     */
    addSelectedMemberListener();
    populateComboBox(context.getService().getCurrentFamily().getGtx_membersList().filtered(sexFilter(Sex.FEMALE)), simLeftChoice);
    populateComboBox(context.getService().getCurrentFamily().getGtx_membersList().filtered(sexFilter(Sex.MALE)), simRightChoice);
    populateComboBox(context.getService().getCurrentFamily().getGtx_membersList()
            .filtered(p -> context.getService().getCurrentFamily().getBornRelation(p).isRacine()), childChoice);

    simLeftChoice.getSelectionModel().selectFirst();
    simRightChoice.getSelectionModel().selectFirst();
    childChoice.getSelectionModel().selectFirst();

    log.trace(LogMessages.MSG_CTRL_INITIALIZED);
}

I have an error in line :

    populateComboBox(context.getService().getCurrentFamily().getGtx_membersList()
            .filtered(p -> context.getService().getCurrentFamily().getBornRelation(p).isRacine()), childChoice);

Here is the stack trace :

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.genealogytree.client.desktop.controllers.implementation.TabAddNewRelationPaneController.lambda$initialize$2(TabAddNewRelationPaneController.java:143)
    at javafx.collections.transformation.FilteredList.addRemove(FilteredList.java:268)
    at javafx.collections.transformation.FilteredList.sourceChanged(FilteredList.java:144)
    at javafx.collections.transformation.TransformationList.lambda$getListener$15(TransformationList.java:106)
    at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
    at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
    at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
    at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
    at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
    at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
    at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
    at java.util.AbstractList.add(AbstractList.java:108)
    at com.genealogytree.client.desktop.domain.GTX_Family.addMember(GTX_Family.java:55)
    at com.genealogytree.client.desktop.service.implementation.LocalFamilyService.addMember(LocalFamilyService.java:71)
    at com.genealogytree.client.desktop.controllers.implementation.TabAddNewMemberPaneController.addSimConfirm(TabAddNewMemberPaneController.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    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.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:8413)
    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.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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)

The problem is not this error but the fact that this fragment of code is called

The log :

14:53:30.453 TRACE .c.d.c.i.TabAddNewRelationPaneController : Controller Initialized correctly
14:53:31.730 TRACE .c.d.c.i.TabAddNewRelationPaneController : Finalize
14:53:33.758 TRACE .g.c.d.c.i.TabAddNewMemberPaneController : Initialization controller
14:53:33.760 TRACE .g.c.d.c.i.TabAddNewMemberPaneController : Controller Initialized correctly
14:53:39.075 INFO  c.g.c.d.s.i.LocalFamilyService           : Add new Member [GTX_Member{version=LongProperty [value: 0], id=LongProperty [value: 0], name=StringProperty [value: jiji], surname=StringProperty [value: ijij], photo=StringProperty [value: null], age=YOUNG_ADULT, sex=MALE}]
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
14:53:39.076 INFO  c.g.c.d.s.i.LocalFamilyService           : Add new Member [GTX_Member{version=LongProperty [value: 0], id=LongProperty [value: 2], name=StringProperty [value: jiji], surname=StringProperty [value: ijij], photo=StringProperty [value: null], age=YOUNG_ADULT, sex=MALE}]
    at com.genealogytree.client.desktop.controllers.implementation.TabAddNewRelationPaneController.lambda$initialize$2(TabAddNewRelationPaneController.java:143)

I've been proceed some more test.

I changed a litle bit the code like this :

    populateComboBox(context.getService().getCurrentFamily().getGtx_membersList().filtered(this::isRacine), childChoice);


public boolean isRacine(GTX_Member p) {
    System.out.println("IS RACINE CALLED");
    log.error("RACINE LAMBDA CALLED");
    return context.getService().getCurrentFamily().getBornRelation(p).isRacine();
}

And now I was do debug 2 cases : a) breakpoint on line populate combobox b) breakpoint on predicate

As fabian mentionned the programm stopped at predicate only (breakpoint on line didn't cause the stop execution)

But I'm still don't understand why it is like this. At this moment this controller is not active. It seems there are an Listener on ObservableList of Member..

So, how can I remove this listener when calling Cancel method ?

VANILKA
  • 634
  • 1
  • 13
  • 32
  • 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 Apr 01 '17 at 12:10
  • BTW: It's not the `initialize` method that is called, but the lambda expression's body causes the in the NPE... – fabian Apr 01 '17 at 12:12
  • As I told, the problem is not NPE, but the fact of run this function. NPE is because isRacine method is called when the relation is null. Normaly i getBornRelation it was a code which create Relation Racine if not exist. But in this case, when i open and close 5 time "Add new relation" window, after add new Member, this function wil be called 5 time. and this is the ROOT problem. Why this method is called whith operation add new member. There is no bridge bettwen New Member and New Relation controllers – VANILKA Apr 01 '17 at 12:46
  • @fabian but why, the lambda is called here ?? the Add Relation windows is no more on the scene. Whene I open and close 5 times Add Relation, this lambda will be calle 5 times, and caused 5 NPE.. Before in the getBornRelation i had method for creation bornRelation if not exist, that caused duplication of born relations. – VANILKA Apr 01 '17 at 15:09
  • Do you know what a lambda does? it's just a shorthand in-line method. context.getService().getCurrentFamily().getGtx_membersList() .filtered(p -> context.getService().getCurrentFamily().getBornRelation(p).isRacine()) So for its first parameter, populate combo box is getting .. I dont know where `p` is defined. First p is put inside context.getService().getCurrentFamily().getBornRelation(p).isRacine() Then whatever that method isRacine returns is put into context.getService().getCurrentFamily().getGtx_membersList() .filtered(-here-) And lastly the result – CausingUnderflowsEverywhere Apr 01 '17 at 15:47
  • of the filtered is used at the first parameter of the populateComboBox method. Have you confused lambda expression being only used to represent runnables? They can be used with runnables but they are simply short hand for a method. In runnables you pass a method that will get executed later. But in your case you're passing a method that will get executed now. Otherwise, I don't know how your program is working, so I can't help you. – CausingUnderflowsEverywhere Apr 01 '17 at 15:48
  • context.getService().getCurrentFamily().getGtx_membersList . return GTX_Member list right ? so p represent a GTX_Member object, so filtered wil return all objects . for laquelles getBornRelation is the Racine. But the problem is that this lamba is run when I use another Controller which isn't connected to this one – VANILKA Apr 01 '17 at 16:44

0 Answers0