Pergunta

I'd like to use the CompoundPropertyModel in Wicket for creating a user.

My user class looks like this:

    public class User {
      private String username;
      ...
      private Address address;
      ...
     }

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

If I try to access the street of the address via the User's compoundproperty model, I get a nullpointerexception, of course: "user.address.street". So I have to instantiate the class "Address" on my own in advance. Is there a more elegant way to dynamically instantiate member fields?

Thanks

Foi útil?

Solução

If a User must have an Address, you should create the instance of Address in the constructor for the User. Otherwise, you might do a null check in your getAddress() method and create a new instance if it's null...

public Address getAddress() {
    if (address == null) {
        address = new Address();
    }

    return address;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top