0

NullPointerException当我尝试通过在一些容器管理的对象中创建自定义映射器来添加映射时,我得到了。我在@Stateless EJB 服务和@RequestScoped 休息服务中尝试过。错误是相似的并且发生在一行中:

modelMapper.addMappings(skipCdeMap);

它是一个错误吗?我假设 mapper 正在尝试在托管 bean 上做一些反射工作,因此 get NullPointerException

这是我的代码:

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

modelMapper.addMappings(skipCdeMap);

这是错误:

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
4

2 回答 2

3

我可以回答我自己的问题。我的假设是正确的。当我在 Pojo 或它自己的专用类中创建 PropertyMap 类时,映射工作得很好。所以我想我之前的错误是由于 ModelMapper 在创建 PropertyMap 的类上所做的反射而发生的。如果是一些会导致 NullPointerException 的 Menaged Bean。

于 2014-10-07T23:09:39.270 回答
3

我有同样的问题(使用 ModelMapper v0.7.5)试图映射StringUUID.

例如,有这些类:

public class Car {

    private UUID id;

    // other fields

}

public class CarDto {

    private String id;

    // other fields

}

并尝试像这样设置自定义映射:

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

我收到相同的配置错误。

原因是这UUID是一个最终类,不能由 ModelMapper 框架代理。

作为一种解决方法,我在我的Car班级中创建了一个这样的方法:

public void setIdAsString(String id) {
    this.id = UUID.fromString(id);
}
于 2016-04-12T10:57:54.380 回答