我正在涉足 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 后,它会抛出一个错误,让我相信我的模型没有被注入(inject),至少在绑定(bind)发生时是这样。
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启动应用程序。
编辑:所以我一直在调试,看起来模型正在被注入(inject),但是在设置绑定(bind)时出现了问题。在 StringPropertyBase 类中,传递给它的 newObservable 的值为 null。
public void bind(ObservableValue<? extends String> newObservable) {
if (newObservable == null) {
throw new NullPointerException("Cannot bind to null");
}
我不知道为什么会发生这种情况,也不知道如何解决它=(
最佳答案
问题出在下面一行
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());
关于java - 格里芬-JavaFX : View throws NPE concerning the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471605/