Vaadin 8 nested properties

Binding to a nested property seems only to work with lambda expressions, not with property names (String) or method reference.
My Problem is that the nested property can be NULL. In this case, the lambda expressions throw a null pointer exception.

What is the Vaadin way to handle this problem?

Hi Karl,

Could you please post some code sample so it would be easier to figure out what is happening?

For a nested property, you could do something like:

public class Address {
 private String street;
 
 ...
}
 
public class Person {

  private String name;
  private Address address;

  ...
}

// binding to street
Binder<Person> binder = new Binder<>();

TextField streetField = new TextField();

binder.forField(streetField).bind(
  person -> {
    if (person.getAddress() != null) {
		return person.getAddress().getStreet();
	}
	return ""; //if address is null, return empty string
  },
  (person, street) -> {
    if (person.getAddress() != null) {
	  person.getAddress().setStreet(street);
	} else {
	  // i.e. create a new address object
	  Address address = new Address();
	  address.setStreet(street);
	  person.setAddress(address)
	}
  });

( disclaimer: I haven’t tested this code in an IDE, so there might be some mistakes :slight_smile: )

Hope this helps,
Goran