0

A very simple problem which smoked holes in my case. I'm trying to skip userDTO's setPassword every time when I convert User --> UserDTO.

I get NullPointerException, at this line in ModelMapper -

I understand the 'source' mapping is not present but my question is why does it even care about it since I asked it to not set the password at all.

Sorry, I think I'm lacking some basics here with ModelMapper. Thank you for your time and help.

Got help from ModelMapper skip a field and few other links but no luck.

enter image description here

Below is the code

@Bean
public ModelMapper modelMapper() {
    ModelMapper mm = new ModelMapper();
    mm.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    TypeMap<User, UserDTO> userEntityToDTOMap = mm.createTypeMap(User.class, UserDTO.class);
    userEntityToDTOMap.addMappings(a -> a.skip(UserDTO::setPassword));

    return mm;
}

public static void main(String[] args) {
    BootstrapConfigurationManager mgr = new BootstrapConfigurationManager();
    ModelMapper mm = mgr.modelMapper();
    mm.getConfiguration().setPropertyCondition(Conditions.isNotNull());

    User user = new User();
    user.setId(44L);
    user.setPassword("password");

    UserDTO userDTO = new UserDTO();
    userDTO.setEmailAddress("abc@abc.com");

    mm.map(user, userDTO);

    System.out.println(userDTO.getId());
    System.out.println(userDTO.getEmailAddress());
    System.out.println(userDTO.getPassword());
}
Sanjeev Dhiman
  • 1,169
  • 1
  • 11
  • 20

1 Answers1

0

Ah !!! It was a bug in v 1.1.0. I just upgraded it to 2.3.2 and I no longer get NPE. I should have thought of upgrading the version before posting this question.

Sanjeev Dhiman
  • 1,169
  • 1
  • 11
  • 20