3

After upgrading Java version to openJDK 11, modelMapper configurations are not getting loaded, getting NullPointerException.

NullPointerException issue is not resolved even after upgrading modelMapper version to 2.3.2

Error Log:

1) Failed to configure mappings

Stack trace:

at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:241)
    at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:244)
    at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:96)
    at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:92)
    at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:124)
    at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:113)
    ...
Caused by: java.lang.NullPointerException
    at org.modelmapper.internal.ExplicitMappingBuilder$ExplicitMappingInterceptor.access$000(ExplicitMappingBuilder.java:304)
    at org.modelmapper.internal.ExplicitMappingBuilder.createAccessorProxies(ExplicitMappingBuilder.java:287)
    at org.modelmapper.internal.ExplicitMappingBuilder.createProxies(ExplicitMappingBuilder.java:277)
    at org.modelmapper.internal.ExplicitMappingBuilder.visitPropertyMap(ExplicitMappingBuilder.java:266)
    at org.modelmapper.PropertyMap.configure(PropertyMap.java:386)
    at jdk.internal.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.modelmapper.internal.ExplicitMappingBuilder.build(ExplicitMappingBuilder.java:227)
    ... 6 more
Shankar
  • 31
  • 1
  • 3
  • Could you share a snippet of one of your models and also the mappings for reproducing the issue on my machine as well. Looking at the stacktrace it's worth mentioning the access modifiers of the classes as well as the location (package name or inner class) – käyrätorvi Jun 28 '19 at 13:29
  • @käyrätorvi It's giving NullPointerException at line map().setLastModifiedDate(source.getLastModifiedDate().getTime()); Here source LastModifiedDate is of type java.util.Date and destination is of type Long – Shankar Jul 01 '19 at 11:30

2 Answers2

1

Internally ByteBuddy is throwing an exception which causes the issue, I will look into it a bit more to see if is perhaps a bug in ModelMapper.

Anyway check out the Java 8 tab at http://modelmapper.org/user-manual/property-mapping/, your current code seems to be using the older documentation.

ModelMapper mm = new ModelMapper();

TypeMap<A, B> typeMap = mm.createTypeMap(A.class, B.class);
typeMap.addMappings(mapper -> {
    mapper.map(A::getDate, B::setTest);
    ... // Other mappings
});

This can convert Date -> Long out of the box without any other configuration. If you wish to configure it further you can always create custom Converters like so:

Converter<Date, Long> dateToLong = new AbstractConverter<Date, Long>() {
    @Override
    protected Long convert(Date source) {
        System.out.println("converting: " + source + " to " + source.getTime());
        return source.getTime();
    }
};

typeMap.addMappings(mapper -> mapper.using(dateToLong).map(A::getDate, B::setTest));
käyrätorvi
  • 371
  • 1
  • 9
  • There's already an issue created https://github.com/modelmapper/modelmapper/issues/482 – Alfergon Feb 12 '20 at 16:24
  • I've made a fix that works for me in a forked repo that I requested to pull https://github.com/modelmapper/modelmapper/pull/522 – Alfergon Feb 14 '20 at 10:49
1

I had the same issue with modelmapper 2.3.0. Updated to latest 2.3.9 and issue was resolved

praveen
  • 19
  • 2