简体   繁体   中英

How to copy a generic list with Eclipse EMF?

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.

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<>();

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM