sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
495 stars 104 forks source link

Default value for the ModelWrapper field method #601

Closed svedie closed 5 years ago

svedie commented 5 years ago

When I define a ModelWrapper in the ViewModel:

ModelWrapper<MyObject> wrapper = new ModelWrapper<>();

all values of MyObject are initialized with null.

Now, when I use the wrapper to retreive the values and they are null, an NullPointerException is thrown.

This definition of an observable list property:

ObservableList<String> numberProperty() {
   return wrapper.field("number", MyObject::getNummer, MyObject::setNummer);
}

throws an NullPointerException if I use it to initialize an ComboBox or an ListView.

It would be helpfull, if one could define an default value in the ModelWrapper for the getter method. If there is no default value is defined, then the behavior is as now implemented.

manuel-mauky commented 5 years ago

Hi, this is already possible. There are overloaded field methods that are taking an additional argument for the default value. See this example:

public StringProperty nameProperty() {
    return wrapper.field("name", MyObject::getName, MyObject:setName, "default value here");
}

For your example with an observable list it would be:

ObservableList<String> numberProperty() {
   return wrapper.field("number", MyObject::getNummer, MyObject::setNummer, new ArrayList<>());
}
svedie commented 5 years ago

Thanks!

I missed this one method :(