程序运行方法意外

时间:2017-04-01 12:01:35

标签: javafx

我有以下问题。

我有2个FXML控制器。 1.对于添加会员 2.对于添加关系

单击主程序create instance od second controller中的Add Relation Button。

在我添加新会员后运行程序一切正常。当我点击添加关系然后单击按钮取消或接受,接下来我去添加新成员我做SUBMIT,调用第二个控制器的初始化方法。我不明白为什么

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

我有一个错误:

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

这是堆栈跟踪:

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)

问题不在于这个错误,而在于这个代码片段被称为

日志:

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)

我已经进行了一些测试。

我改变了这样的代码:

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

现在我做了调试2个案例: a)断点在线填充组合框 b)关于谓词的断点

由于fabian提到程序只在谓词停止(在线断点不会导致停止执行)

但我仍然不明白为什么会这样。此时此控制器未激活。似乎会员的ObservableList上有一个Listener ..

那么,如何在调用Cancel方法时删除此侦听器?

0 个答案:

没有答案