{"record":{"id":"7e3be32ea7ee696e","repo":"dotnet/wpf","slug":"sr-textrunpropertiescannotbenull","errorCode":null,"errorMessage":"SR.TextRunPropertiesCannotBeNull","messagePattern":"SR\\.TextRunPropertiesCannotBeNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextRunCacheImp.cs","lineNumber":115,"sourceCode":"            _latestPosition = textRunSpanRider.SpanPosition;\n            TextRun textRun = (TextRun)textRunSpanRider.CurrentElement;\n\n            if(textRun == null)\n            {\n                // run not already cached, fetch new run and cache it\n\n                textRun = settings.TextSource.GetTextRun(cpFetch);\n\n                ArgumentOutOfRangeException.ThrowIfNegativeOrZero(textRun.Length, \"textRun.Length\");\n\n                Plsrun plsrun = TextRunInfo.GetRunType(textRun);\n\n                if (plsrun == Plsrun.Text || plsrun == Plsrun.InlineObject)\n                {\n                    TextRunProperties properties = textRun.Properties;\n\n                    if (properties == null)\n                        throw new ArgumentException(SR.TextRunPropertiesCannotBeNull);\n\n                    if (properties.FontRenderingEmSize <= 0)\n                        throw new ArgumentException(SR.Format(SR.PropertyOfClassMustBeGreaterThanZero, \"FontRenderingEmSize\", \"TextRunProperties\"));\n\n                    double realMaxFontRenderingEmSize = Constants.RealInfiniteWidth / Constants.GreatestMutiplierOfEm;\n\n                    if (properties.FontRenderingEmSize > realMaxFontRenderingEmSize)\n                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeGreaterThan, \"FontRenderingEmSize\", \"TextRunProperties\", realMaxFontRenderingEmSize));\n\n                    CultureInfo culture = CultureMapper.GetSpecificCulture(properties.CultureInfo);\n\n                    if (culture == null)\n                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeNull, \"CultureInfo\", \"TextRunProperties\"));\n\n                    if (properties.Typeface == null)\n                        throw new ArgumentException(SR.Format(SR.PropertyOfClassCannotBeNull, \"Typeface\", \"TextRunProperties\"));\n                }\n","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/TextFormatting/TextRunCacheImp.cs#L97-L133","documentation":"During FetchTextRun the run cache validates each TextRun's properties. It throws ArgumentException(SR.TextRunPropertiesCannotBeNull) when TextRun.Properties is null: every run must carry non-null TextRunProperties for formatting to proceed.","triggerScenarios":"A custom TextSource returning a TextRun whose Properties property is null, reached when TextFormatter's run cache fetches that run.","commonSituations":"Custom TextRun implementations forgetting to set Properties; TextRun wrappers that pass through null from external data; code that clears run properties after construction.","solutions":["Return a TextRun whose Properties is a valid TextRunProperties instance from your TextSource","Validate run properties in your TextRun constructor before returning it","Throw a clearer exception in your TextSource early rather than returning an unconfigured run"],"exampleFix":"// before\npublic override TextRun GetTextRun(int i) => new TextCharacters(\"abc\", null);\n// after\npublic override TextRun GetTextRun(int i) => new TextCharacters(\"abc\", new TextRunProperties(typeface, 12.0));","handlingStrategy":"type-guard","validationCode":"var run = source.GetTextRun(pos);\nif (run?.Properties == null) throw new InvalidOperationException(\"TextRun must have non-null Properties\");","typeGuard":"bool HasProperties(TextRun r) => r?.Properties != null;","tryCatchPattern":"try { formatter.FormatLine(...); } catch (ArgumentException) { /* fix TextSource to return runs with properties */ }","preventionTips":["Always populate Properties in every TextRun your TextSource returns","Test your TextSource with a formatter pass before production"],"tags":["wpf","text-formatting","invalid-argument","textrun"],"backgroundTag":"invalid-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}