Slide cloning error. NPE at at com.aspose.slides.Presentation.cloneSlide(SourceFile:1599)

Hi, I was wonderig if you could tell me, what could cause an NPE at com.aspose.slides.Presentation.cloneSlide(SourceFile:1599).

I'm using Aspose Slides for Java, aspose-slides-2.1.0.3-20100209.jar.

I don't want to roll back all my code changes.

I'm meanwhile very much concerned about this lib is not being able to handle any null value as means to say "nothing". Whenever I accidently set a name or something else somewhere with the value null, then Aspose is not immediately complaining but crashing sometime later with an NPE. But since all is obfuscated, you don't get any idea of what may have caused it.

Please give me a quick hint where to look.


It may have been cause by setting a masterId within a previous slide copy action. My intention is to refactor slides to use a common master slide.

Hi Jens,

We are extremely sorry for your inconvenience.

In order to verify and reproduce the problem of null pointer exception on my end, please share the source presentation data. This way we can help you out of the inconvenient situation.

Thanks and Regards,

Hi, in this case, it would be nearly impossible to share the source presentation data. I assemble certain components from different sources an get that error in a cloning operation. But this cloning does not fail, if I didn’t set the masterId to a slide right after a prior cloning. So, if I take that setMasterId() out, it works.

Thus I think, from the source line I gave you, you should be able to see, what the code is trying to access and wether it may be null.

Down is my copy - -method: see the commented line with setMasterId.

I get the masterId, I want to set to any slide, by:

long mainMasterId = targetReport.getSlideByPosition(1).getMasterId();

What would be the difference between the line above, which returns a non-zero long and the line

targetReport.getMainMaster().getMasterId(); ???

The documentation again is thin here.



Additionally, I did not find any hint in the doc, how to deal with the last parameter of cloneSlide(), that TreeMap. Could you please point me to an example, where it is not empty and why?



protected Slide appendCopyOfSlideToReport(Slide sourceSlide, Presentation targetReport) {

// now copy this slide to the result object

int targetSlidePosition = targetReport.getSlides().getLastSlidePosition() + 1; // the position where new slide should be inserted

int slideWidth = sourceSlide.getBackground().getWidth();

int slideHeight = sourceSlide.getBackground().getHeight();

Point targetSize = targetReport.getSlideSize();



if (targetSize.getX() < slideHeight) {

targetReport.setSlideSize(new Point(slideWidth, slideHeight));

}

boolean isFromDifferentPresentation = sourceSlide.getParent() != targetReport;

// TODO:2: java.util.TreeMap idList - Is an access to control the masters here?

Slide destSlide = sourceSlide.getParent().cloneSlide(sourceSlide, targetSlidePosition, targetReport, new TreeMap<Object, Object>());

if (sourceSlide.getNotes() != null && sourceSlide.getNotes().getText() != null) {

Notes destNotes = destSlide.getNotes();

if (destSlide.getNotes() == null) {

destNotes = destSlide.addNotes();

}

destNotes.setText(sourceSlide.getNotes().getText());

}

// TODO:1: attach Master from target presentation?, setMaster’s visibility?

if (/isFromDifferentPresentation &&/ mainMasterId != 0L) {

// destSlide.setMasterId(mainMasterId);

}

inheritHeadersFootersVisibility(sourceSlide, destSlide);

return destSlide;

}



Jens

Hi Jens,

We are sorry for your inconvenience.

I have observed the source code and it looks fine. We regret to inform you that with no access given to source presentation data, it will be really difficult for us to identify the issue. We really want to help you out of the problem and need your cooperation in this regard.

Thanks and Regards,

hi, first please answer my detail questions regarding the api and the intention to take over one master for all.i can’t give you more, no idea how.

Dear Jens,

We are sorry for the delayed response but we were working on your query.

Since you have mentioned that it was hard for you to share your presentation data, so we would appreciate, if you could share the stack trace of the exception and may be it proves helpful for us to identify the problem.

You can use one master for cloned slides in destination presentation. For this please use changeMaster method merging slides to one presentation. I have modified the source code provided by you and hopefully it will work as I have tested it with my presentation data. I have also attached the source and resultant PPT files for your reference.

Last parameter of cloneSlide method contains TreeMap as argument, which is used by Aspose.Slides internally for holding shapes identifiers. It should be unique for each source-target presentations pair. So if you clone several slides from one source presentation to one target presentation and use the same TreeMap for that then only one master slide will be copied for all cloned slides. If you use unique TreeMap for each clone operation then new copy of master slide will be copied for each cloned slide.

static Slide appendCopyOfSlideToReport(Slide sourceSlide, Presentation targetReport)
{
    // now copy this slide to the result object
    long mainMasterId = targetReport.getSlideByPosition(1).getMasterId();
    Slide MasterSld=targetReport.getSlideById(mainMasterId);

    int targetSlidePosition = targetReport.getSlides().getLastSlidePosition() + 1; // the position where new slide should be inserted
    int slideWidth = sourceSlide.getBackground().getWidth();
    int slideHeight = sourceSlide.getBackground().getHeight();
    Point targetSize = targetReport.getSlideSize();

    if (targetSize.getX() < slideHeight)
    {
        targetReport.setSlideSize(new Point(slideWidth, slideHeight));
    }

    boolean isFromDifferentPresentation = sourceSlide.getParent() != targetReport;

    // TODO:2: java.util.TreeMap idList - Is an access to control the masters here?

    Slide destSlide = sourceSlide.getParent().cloneSlide(sourceSlide, targetSlidePosition, targetReport, new TreeMap());

    if (sourceSlide.getNotes() != null && sourceSlide.getNotes().getText() != null)
    {
        Notes destNotes = destSlide.getNotes();
        if (destSlide.getNotes() == null)
        {
            destNotes = destSlide.addNotes();
        }

        destNotes.setText(sourceSlide.getNotes().getText());
    }

    // TODO:1: attach Master from target presentation?, setMaster's visibility?
    if (/*isFromDifferentPresentation &&*/ mainMasterId != 0L)
    {
        destSlide.changeMaster(MasterSld,true);
    }

    //inheritHeadersFootersVisibility(sourceSlide, destSlide);
    return destSlide;
}

Thanks and Regards,