0

I get NullPointerException when I try to add mappings by creating my custom mapper in some container-managed objects. I tried it in @Stateless EJB service and @RequestScoped rest service. The error is similar and occurs in line:

modelMapper.addMappings(skipCdeMap);

Is it a bug? I assume that mapper is trying to do some reflection stuff on managed bean and because of that get NullPointerException.

This is my code:

PropertyMap<ObjectAbcDto, ObjectAbc> skipCdeMap =
    new PropertyMap<ObjectAbcDto, ObjectAbc>() {
        protected void configure() {
            skip().setObjectCde(null);
        }
    };

modelMapper.addMappings(skipCdeMap);

This is the error:

13:37:03,453 ERROR [stderr] (default task-12) org.modelmapper.ConfigurationException: ModelMapper configuration errors:
13:37:03,453 ERROR [stderr] (default task-12) 
13:37:03,453 ERROR [stderr] (default task-12) 1) Error reading class com.app.SomeRestEasyService$1
13:37:03,453 ERROR [stderr] (default task-12) 
13:37:03,453 ERROR [stderr] (default task-12) 2) Failed to configure mappings
13:37:03,454 ERROR [stderr] (default task-12) 
13:37:03,454 ERROR [stderr] (default task-12) 2 errors
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:206)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:72)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:101)
13:37:03,454 ERROR [stderr] (default task-12) at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:93)
13:37:03,454 ERROR [stderr] (default task-12) at com.app.SomeRestEasyService.create(SomeRestEasyService.java:129)
13:37:03,454 ERROR [stderr] (default task-12) at com.app.SomeRestEasyService$Proxy$_$$_WeldClientProxy.create(Unknown Source)        
[...]   
13:37:03,459 ERROR [stderr] (default task-12) Caused by: java.lang.NullPointerException
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.validateVisitedMappings(ExplicitMappingBuilder.java:236)
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:227)
13:37:03,459 ERROR [stderr] (default task-12) at org.modelmapper.PropertyMap.configure(PropertyMap.java:380)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
13:37:03,459 ERROR [stderr] (default task-12) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:37:03,460 ERROR [stderr] (default task-12) at java.lang.reflect.Method.invoke(Method.java:606)
13:37:03,460 ERROR [stderr] (default task-12) at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:194)
13:37
davioooh
  • 23,742
  • 39
  • 159
  • 250
Ziemo
  • 941
  • 8
  • 27
  • Are you using 0.7.2? If so, do you happen to have a small test that reproduces the problem that you can post? – Jonathan Oct 01 '14 at 20:17

2 Answers2

3

I can answer on my own question. My assumptions was correct. When I created PropertyMap class in Pojo or in its own, dedicated class - mappings worked well. So I suppose that my previous error occurs because of reflection which ModelMapper does on class in which PropertyMap was created. If it is some Menaged Bean that would result with NullPointerException.

Ziemo
  • 941
  • 8
  • 27
  • What did you do to fix it? I think I have the same issue https://stackoverflow.com/questions/53287450/nullpointerexception-when-trying-to-define-a-custom-propertymap – Marc Dec 05 '18 at 13:19
  • Ziemo is actually saying what he did to fix it, but I had to unravel the answer too. You have to create an explicit class and extend from PropertyMap. The ExplicitMappingBuilder has some problems with this. – DKM Oct 16 '19 at 12:55
3

I'm having the same problem (using ModelMapper v0.7.5) trying to map String to UUID.

For example, having these classes:

public class Car {

    private UUID id;

    // other fields

}

public class CarDto {

    private String id;

    // other fields

}

and trying to set a custom mapping like this:

mapper.addMappings(new PropertyMap<CarDto, Car>() {
        protected void configure() {
            map().setId(UUID.fromString(source.getId()));
            // other mappings
        }
    });

I'm getting the same configuration error.

The cause is that UUID is a final class and cannot be proxed by ModelMapper framework.

As a workaround I created in my Car class a method like this:

public void setIdAsString(String id) {
    this.id = UUID.fromString(id);
}
davioooh
  • 23,742
  • 39
  • 159
  • 250