【问题标题】:Griffon-JavaFX: View throws NPE concerning the modelGriffon-JavaFX:视图抛出有关模型的 NPE
【发布时间】:2014-12-15 18:53:13
【问题描述】:

我正在涉足 Griffon 世界,我下载了一个小型的lazybones 应用程序,但它会引发错误。我的看法是这样的。

@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 的原因或任何其他问题提供任何帮助。我相信我从lazybones 中得到的是griffon-javafx-groovy 启动应用程序。

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

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

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

【问题讨论】:

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


    【解决方案1】:

    问题出在下面一行

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 2015-09-30
      • 1970-01-01
      • 2021-01-19
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多