Adding a deep mapping causes a null pointer exception, ModelMapper 1.1.0

2,031 views
Skip to first unread message

richa...@gmail.com

unread,
Oct 10, 2017, 3:52:41 PM10/10/17
to modelmapper
I'm trying to configure a ModelMapper (version 1.1.0) to do a deep mapping between source and destination with the following code:

@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
try {
modelMapper.getConfiguration().setAmbiguityIgnored(true);
Converter<Optional<LocalDate>, LocalDate> toLocalDate =
    ctx -> ctx.getSource() == null ? null : ctx.getSource().orElse(null);
    
modelMapper.addConverter(toLocalDate);
Converter<Optional<StandardId>, String> toIdString = 
ctx -> ctx.getSource() == null ? null : ctx.getSource().map(StandardId::getValue).orElse(null);
modelMapper.addConverter(toIdString);
TypeMap<FxVanillaOption, FxVanillaOptionDto> typeMap = modelMapper.createTypeMap(FxVanillaOption.class, FxVanillaOptionDto.class);
typeMap.addMapping(src -> src.getExpiryZone().getId(), FxVanillaOptionDto::setExpiryZone);
} catch (Exception e) {
e.printStackTrace();
}

    return modelMapper;
}

I'm getting a NPE here in ReferenceMapExpressionImpl where source = null

  public <V> void map(SourceGetter<S> sourceGetter, DestinationSetter<D, V> destinationSetter) {
    PropertyReferenceCollector collector = new PropertyReferenceCollector(typeMap.configuration, options);

    S source = ProxyFactory.proxyFor(typeMap.getSourceType(), collector.newSourceInterceptor(), collector.getErrors());
    sourceGetter.get(source);

    D destination = ProxyFactory.proxyFor(typeMap.getDestinationType(), collector.newDestinationInterceptor(), collector.getErrors());
    destinationSetter.accept(destination, destinationValue(destinationSetter));

    typeMap.addMapping(collector.collect());
  }

My source class doesn't have a default construct (it's an immutable Joda Bean). Is this the cause?


java.lang.NullPointerException
at com.derivativepath.analytics.AnalyticsFxServiceApplication.lambda$3(AnalyticsFxServiceApplication.java:54)
at org.modelmapper.internal.ReferenceMapExpressionImpl.map(ReferenceMapExpressionImpl.java:52)
at org.modelmapper.internal.TypeMapImpl.addMapping(TypeMapImpl.java:231)
at com.derivativepath.analytics.AnalyticsFxServiceApplication.modelMapper(AnalyticsFxServiceApplication.java:54)



richa...@gmail.com

unread,
Oct 11, 2017, 11:50:45 AM10/11/17
to modelmapper
I think this might be due to my source and destination classes being marked as "final". Which leads me to another question: The destination class is not owned by me (it's a 3rd party binary) how can I get around this issue?

Chun Han Hsiao

unread,
Oct 12, 2017, 2:38:01 AM10/12/17
to modelmapper
Hi,

You are right about the finding, our explicit mapping configuration is based on proxying the source/destination class and we cannot proxy a final class so it turns out a NullPointerException.
ModelMapper seems to not have any friendly api to support explicit mapping on a final class.
Currently, I don't have any idea or workaround to solve this problem.. I will think about how ModelMapper support final class explicit mapping..

Regards,
Chun Han

richa...@gmail.com於 2017年10月11日星期三 UTC+8下午11時50分45秒寫道:

Jonathan Halterman

unread,
Oct 12, 2017, 2:44:32 AM10/12/17
to model...@googlegroups.com
Proxying final classes is going to be problematic - it just isn't supported by Cglib (and I'm guessing not by ByteBuddy as well without an agent). That said, we should be able to make a better guess about when proxying fails and report an appropriate error rather than NullPointerException. Without looking at the code - I recall that previously we were detecting NullPointers within a proxy interceptor and reporting an error message that mentioned a possible final method.

--
You received this message because you are subscribed to the Google Groups "modelmapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chun Han Hsiao

unread,
Dec 10, 2017, 9:34:21 PM12/10/17
to modelmapper
I think it might cause by https://github.com/modelmapper/modelmapper/pull/266 that I try resolve https://github.com/modelmapper/modelmapper/issues/260. I will try fix the NullPointerException problem, and consider to use ByteBuddy instead of CGLIB.

Jonathan Halterman於 2017年10月12日星期四 UTC+8下午2時44分32秒寫道:
To unsubscribe from this group and stop receiving emails from it, send an email to modelmapper...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages