map object with parameters in the constructor

844 views
Skip to first unread message

xunin...@gmail.com

unread,
Aug 15, 2013, 8:41:34 AM8/15/13
to model...@googlegroups.com
public class Order{

    private String name;
    private boolean isPostFree;
    private String category;


    public Order(@Nonnull Foo foo){

        this.name=foo.getName();
    }

}


If i want to map Order to OrderDTO, i will get the following error:

Caused by: java.lang.NullPointerException
at Order.<init>(Order.java:17)
at Order$$EnhancerByModelMapper$$b67fffd9.<init>(<generated>)



In my codes i am foo won't be null, so i don't want to add a null check in the constructor. what shall i do?

Jonathan Halterman

unread,
Aug 15, 2013, 8:59:48 PM8/15/13
to model...@googlegroups.com
Hi - I suspect I know what is going on, but can you post the PropertyMapping you created? That would be helpful to see.

Thanks,
Jonathan

Jonathan Halterman

unread,
Aug 15, 2013, 9:00:20 PM8/15/13
to model...@googlegroups.com
I mispoke - could I see the PropertyMap you created.

xunin...@gmail.com

unread,
Aug 15, 2013, 9:58:44 PM8/15/13
to model...@googlegroups.com
Just create a empty map will raise this issue.


 PropertyMap<Order, OrderDTO> orderMap = new PropertyMap<Order, OrderDTO>() {
            protected void configure() {
             //nothing here.
            }
        };
Message has been deleted

xunin...@gmail.com

unread,
Aug 15, 2013, 10:03:26 PM8/15/13
to model...@googlegroups.com

Jonathan Halterman

unread,
Aug 16, 2013, 7:08:26 PM8/16/13
to model...@googlegroups.com
Hi Junin,

What's happening is when you add your orderMap to ModelMapper, it attempts to construct a proxy for Order and OrderDTO in order to be able to capture any mappings you define. In order to construct the proxy ModelMapper calls Order's constructor, passing default values in for all of the arguments,in this case passing in null as the value for foo. We could do something more sophisticated such as construct an instance of Foo and pass that in, but ultimately there's no way to predict how a constructor will behave based on the default values that ModelMapper passes in. The goal here is just to construct Order temporarily so that we can capture any mappings defined for it.

This is an area of ModelMapper that could use some consideration, but I'm not sure if it's necessarily a good idea for ModelMapper to construct arguments to pass to a constructor in case the constructor tries to use those arguments since there's still no way of ensuring that the constructor won't fail for some other reason. 

Ultimately the best workaround for now is to define a default constructor on Order and OrderDTO to ensure that ModelMapper can construct them without any problems.

Cheers,
Jonathan

xunin...@gmail.com

unread,
Aug 17, 2013, 10:47:24 PM8/17/13
to model...@googlegroups.com
Ok, i get it. This is one kind of constraint added by modelMapper.

Thanks for your answering.
Reply all
Reply to author
Forward
0 new messages