绑定中的NullPointerException [英] NullPointerException in binding
问题描述
您好我想根据其他对象绑定一个值。此对象为null,默认情况下将设置该值。但我仍然收到NullPointerException。我希望它会被.then(...)捕获。但事实并非如此
Hi I would like to bind a value depending other object. It this object is null the value will be set by default. But I still received NullPointerException. I expect that it will be catch by ".then(...) . but it is not the case
relationType.bind(Bindings.when(Bindings.createBooleanBinding(() -> ( relation == null || relation.get()== null), relation))
.then(RelationType.NEUTRAL)
.otherwise(relation
.get()
.typeProperty()));
当我向Listener内容添加绑定时,一切正常:
All works fine when I add binding to Listener content :
relation.addListener((observable, oldValue, newValue) -> {
if(newValue != null) {
relationType.bind(Bindings
.when(relation.isNull())
.then(RelationType.NEUTRAL)
.otherwise(newValue.typeProperty()));
} else {
relationType.unbind();
relationType.setValue(RelationType.NEUTRAL);
}
});
但我更喜欢o nly绑定。那可能吗?
But I prefere to have only binding. That is possible ??
推荐答案
问题是 relation.get()的时间.typeProperty()
得到评估。它在创建绑定时进行评估,而不是每次 relation
更改时进行评估。你可以使用select绑定解决这个问题,但是你会在控制台中使用这种方法收到警告:
The problem is the time when relation.get().typeProperty()
gets evaluated. It's evaluated when the binding is created and not every time relation
changes. You could work around this using a select binding, but you'll receive warnings in the console using this approach:
relationType.bind(Bindings.when(relation.isNull())
.then(RelationType.NEUTRAL)
.otherwise(Bindings.select(relation, "type"));
这篇关于绑定中的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!