How to copy a generic list with Eclipse EMF?

334 views Asked by At

how can i split an Elist to two Elists without getting the NullPointerException. I already tried EcoreUtil.copy() / Collections.copy. The Problem seems that when declaring the copy target List it needs to be initialized with = null; I also tried using an Iterator to copy Elements and tried adding them with .set() .add() all exit with the Exception above. The declaration of the target List seems to be working only with an allocation. While debugging i clearly see that the copied object in the List is not null.

 EList<RtTask> tasks = rtModule.getTasks();
 EList<RtModuleInvocation> invoc0 = null; //target List
        for (RtTask rtTask : tasks) {
            EList<RtModuleInvocation> invocations = rtTask.getModuleInvocations(); //src List

Thanks.

2

There are 2 answers

0
AudioBubble On

Thanks to https://www.programcreek.com/java-api-examples/emf i found out the correct way to initialize my Elist with a constructor that Creates an empty instance with no initial capacity.The data storage will be null. and HOP it works.

EList<RtModuleInvocation> invoc0 = new BasicEList<>();
0
kapex On

If you want a copy of a list, you can also use the ECollections utility:

ECollections.newBasicEList(Iterable)
Creates a mutable BasicEList containing the given elements.

So to copy the RtModuleInvocation list you can use:

ECollections.newBasicEList(rtTask.getModuleInvocations())