Skip to content

TextBox.SetText throws NullReferenceException on inherited placeholder font #1284

@slopez37-eng

Description

@slopez37-eng

Summary

TextBox.SetText(...) can throw a NullReferenceException when called on placeholder text in a branded PowerPoint template.

This happens when the placeholder's font formatting is inherited through the slide layout/master/theme instead of being directly present on the text run.

Version

  • ShapeCrawler: 0.79.1
  • .NET: 10.0
  • OS: Linux

Repro

using ShapeCrawler;

var templatePath = "template.pptx";

var pres = new Presentation(templatePath);

for (int i = pres.Slides.Count(); i >= 1; i--)
{
    pres.Slide(i).Remove();
}

var layout = pres.MasterSlides[0].SlideLayout("Title and Content");
pres.Slides.Add(layout.Number);

var slide = pres.Slide(pres.Slides.Count());
var titleShape = slide.Shapes.First(s => s.PlaceholderType == PlaceholderType.Title);

titleShape.TextBox!.SetText("Hello");

Actual Behavior

SetText throws:

System.NullReferenceException: Object reference not set to an instance of an object.
   at ShapeCrawler.Shapes.ReferencedFont.SlideALatinFontOrNull(SlidePart sdkSlidePart)
   at ShapeCrawler.Shapes.ReferencedFont.ALatinFontOrNull()
   at ShapeCrawler.Fonts.TextPortionFont.ALatinFont()
   at ShapeCrawler.Fonts.TextPortionFont.get_LatinName()
   at ShapeCrawler.Texts.TextContent.GetLatinNameToPreserve(IParagraph firstParagraph)
   at ShapeCrawler.Texts.TextContent.ApplyTo()
   at ShapeCrawler.Texts.TextBox.SetText(String text)

Expected Behavior

TextBox.SetText(...) should update the placeholder text without crashing.

If ShapeCrawler cannot resolve an inherited placeholder font, it should fall back gracefully instead of throwing.

Investigation Notes

I tested this against ShapeCrawler 0.79.1 and reproduced the crash.

The crash appears to happen in the font-preservation path used by SetText.

Relevant area in v0.79.1:

var refMasterPShapeOfLayout = this.ReferencedMasterPShapeOrNull(refLayoutPShape);
var masterFontsOfLayout = new IndentFonts(refMasterPShapeOfLayout!.TextBody!.ListStyle!);

ReferencedMasterPShapeOrNull(refLayoutPShape) can return null, but the result is dereferenced with !.

Because of that, the code crashes before it can fall back to the theme font.

Workaround

This avoids the crashing SetText path:

shape.TextBox!.Paragraphs.First().Text = "Hello";

However, this workaround does not clear extra placeholder/demo paragraphs, so callers may also need:

foreach (var p in shape.TextBox!.Paragraphs.Skip(1).ToList())
{
    p.Remove();
}

shape.TextBox.Paragraphs.First().Text = "Hello";

Related Issue

This may be related to #638 because both involve replacing placeholder text and preserving formatting, but this issue is a hard crash rather than a font-size change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions