Reputation: 684
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()));
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);
}
});
But I prefere to have only binding. That is possible ??
Upvotes: 1
Views: 687
Reputation: 82461
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"));
Upvotes: 2