Invalid Operation Error while Cloning PowerPoint Presentation Slides in Java

Getting this error while trying to add clone slide :

Presentation master= new Presentation(this.getClass().getClassLoader().getResourceAsStream("src/main/resources/slide.pptx"));

Presentation presentation = new Presentation(); 
presentation.getSlides().removeAt(0);
presentation.getSlides().addClone(master.getSlides().get_Item(0));

I am passing the master slide to several methods and executing those methods in parallel. I don’t get any issue when executing them sequentially.
com.aspose.slides.exceptions.InvalidOperationException: Trying to add new cached value with the same keys.

@agarwalsarthak121

There is issue in your used code. You are cloning the master slide in normal slide with is not right. You need to use the correct slide type for cloning is respecting collection.

Thanks. I was able to figure out the issue.

@agarwalsarthak121

It’s good to know things are working fine on your end as per suggestion.

Hi, what is the solution to this issue? I am getting the same error. What do you mean by normal slide?

Presentation sourcePPT = new Presentation(ResourceUtil.getResourceAsStream("…potx"));
Presentation destinationPPT = new Presentation();
ISlideCollection slides = destination.getSlides();
slides.insertClone(slides.size(), source.getSlides().get_Item(indexOfSourceSlide));

Error:
Caused by: class com.aspose.slides.exceptions.InvalidOperationException: Trying to add new cached value with the same keys.
com.aspose.slides.EffectiveDataCache.do(Unknown Source)
com.aspose.slides.char.do(Unknown Source)
com.aspose.slides.apd.for(Unknown Source)
com.aspose.slides.TextFrameFormat.while(Unknown Source)
com.aspose.slides.AutoShape.else(Unknown Source)
com.aspose.slides.Shape.finally(Unknown Source)
com.aspose.slides.Transform2DPPTXSerialization.do(Unknown Source)
com.aspose.slides.Transform2DPPTXSerialization.do(Unknown Source)
com.aspose.slides.akm.do(Unknown Source)
com.aspose.slides.qo.do(Unknown Source)
com.aspose.slides.r.do(Unknown Source)
com.aspose.slides.rr.do(Unknown Source)
com.aspose.slides.rr.do(Unknown Source)
com.aspose.slides.bj.do(Unknown Source)
com.aspose.slides.aau.if(Unknown Source)
com.aspose.slides.agq.do(Unknown Source)
com.aspose.slides.agq.do(Unknown Source)
com.aspose.slides.agf.do(Unknown Source)
com.aspose.slides.SlideCollection.addClone(Unknown Source)
com.aspose.slides.SlideCollection.addClone(Unknown Source)
com.aspose.slides.SlideCollection.addClone(Unknown Source)
com.aspose.slides.SlideCollection.insertClone(Unknown Source)

FYI … my code is multi threaded.

I have a static instance of the sourcePPT object, and one instance per thread of the destinationPPT object. And this error happens randomly, not all the time.

@hese2428,
Thank you for the query. Unfortunately, cloning slides is not a safe operation for multi-threaded code. Please take a look at the next article: Multithreading in Aspose.Slides.

@Andrey_Potapov
thanks for the link.
Can I have one sourcePPT Presentation and one destination PPT instance per thread (but within the same java process), so each thread has its own copy of the source Presentation to clone from. Would that cause any issues ?

@hese2428,
With this approach, everything should work without errors.

ok thanks for the confirmation. I tried this, and it seems to be working (no NPE so far)