Griffon-JavaFX:View会抛出有关模型的NPE

时间:2014-10-20 17:37:48

标签: java model-view-controller groovy javafx griffon

我正在进军Griffon世界,我有一个我下载的小型懒人应用程序抛出错误。我的观点是这样的。

@ArtifactProviderFor(GriffonView)
class PennyPackerView {
FactoryBuilderSupport builder
PennyPackerController controller
PennyPackerModel model

void initUI() {
    builder.application(title: application.configuration['application.title'],
        sizeToScene: true, centerOnScreen: true, name: 'mainWindow') {
        scene(fill: WHITE, width: 200, height: 60) {
            gridPane {
                label(id: 'clickLabel', row: 0, column: 0,
                      text: bind(model.clickCount))
                button(row: 1, column: 0, prefWidth: 200,
                        controller.click())
            }
        }
    }
}

}

使用gradle:run之后,它会抛出一个错误,让我相信我的模型没有被注入,至少在绑定发生的时候。

Warning, could not locate neither a JavaFX property nor a JavaBean property for class         
javafx.scene.control.Label, property: '0'
[2014-10-20 13:30:56,166] [JavaFX Application Thread] 
  ERROR griffon.core.GriffonExceptionHandler      
  - Uncaught Exception
 java.lang.NullPointerException: Cannot bind to null
at javafx.beans.property.StringPropertyBase.bind(StringPropertyBase.java:161)
at javafx.beans.property.Property$bind.call(Unknown Source)
at groovyx.javafx.factory.FXHelper$__clinit__closure26.doCall(FXHelper.groovy:454)

以下是型号代码,它不是非常复杂。

@ArtifactProviderFor(GriffonModel)
class PennyPackerModel {
    @FXObservable String clickCount = "0"
}

非常感谢任何有关此NPE被抛出或任何其他问题的帮助。我相信我从懒人那里得到的是griffon-javafx-groovy的首发应用程序。

编辑:所以我一直在调试,看起来正在注入模型,但是在设置绑定时出现了问题。在StringPropertyBase类中,传递给它的newObservable的值为null。

    public void bind(ObservableValue<? extends String> newObservable) {
    if (newObservable == null) {
        throw new NullPointerException("Cannot bind to null");
    }

我不知道为什么会这样,或者如何解决它=(

1 个答案:

答案 0 :(得分:0)

问题是以下一行

text: bind(model.clickCount))

应该是

text: bind(model.clickCountProperty))

第一个返回一个普通值,而第二个返回一个JavaFX属性。我很惊讶你发现这个问题,因为对源代码的快速grep会产生以下结果

$ grep clickCount -r griffon-javafx-* | grep bind
griffon-javafx-groovy-templates/templates/griffon-javafx-groovy/griffon-app/views/View.groovy:                          text: bind(model.clickCountProperty()))
griffon-javafx-groovy-templates/templates/subtmpl-artifact/View.groovy:                          text: bind(model.clickCountProperty()))
griffon-javafx-java-templates/templates/griffon-javafx-java/griffon-app/views/View.java:        model.clickCountProperty().bindBidirectional(clickLabel.textProperty());
griffon-javafx-java-templates/templates/subtmpl-artifact/View.java:        model.clickCountProperty().bindBidirectional(clickLabel.textProperty());