{"record":{"id":"482f9b2208a6c5b5","repo":"dotnet/wpf","slug":"argumentnullexception-characterbufferreference","errorCode":null,"errorMessage":"ArgumentNullException(\"characterBufferReference.CharacterBuffer\")","messagePattern":"ArgumentNullException\\(\"characterBufferReference\\.CharacterBuffer\"\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextCharacters.cs","lineNumber":186,"sourceCode":"        /// Shape items are delimited by \n        ///     Change of writing system\n        ///     Change of glyph typeface\n        /// </summary>\n        IList<TextShapeableSymbols> ITextSymbols.GetTextShapeableSymbols(\n            GlyphingCache               glyphingCache,\n            CharacterBufferReference    characterBufferReference,\n            int                         length,\n            bool                        rightToLeft,\n            bool                        isRightToLeftParagraph,\n            CultureInfo                 digitCulture,\n            TextModifierScope           textModifierScope,\n            TextFormattingMode          textFormattingMode,\n            bool                        isSideways\n            )\n        {\n            if (characterBufferReference.CharacterBuffer == null)\n            {\n                throw new ArgumentNullException(\"characterBufferReference.CharacterBuffer\");\n            }\n            \n            int offsetToFirstChar = characterBufferReference.OffsetToFirstChar - _characterBufferReference.OffsetToFirstChar;\n\n            Debug.Assert(characterBufferReference.CharacterBuffer == _characterBufferReference.CharacterBuffer);\n            Debug.Assert(offsetToFirstChar >= 0 && offsetToFirstChar < _length);\n\n            if (    length < 0 \n                ||  offsetToFirstChar + length > _length)\n            {\n                length = _length - offsetToFirstChar;\n            }\n\n            // Get the actual text run properties in effect, after invoking any\n            // text modifiers that may be in scope.\n            TextRunProperties textRunProperties = _textRunProperties;\n            if (textModifierScope != null)\n            {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextCharacters.cs#L168-L204","documentation":"TextCharacters.GetTextShapeableSymbols validates that the supplied characterBufferReference.CharacterBuffer is non-null and throws ArgumentNullException(\"characterBufferReference.CharacterBuffer\"). The method maps characters to glyph runs and cannot operate without an underlying character buffer.","triggerScenarios":"Calling GetTextShapeableSymbols with a CharacterBufferReference whose CharacterBuffer is null — e.g. default-constructed CharacterBufferReference, or one built from a null string/array/char* source.","commonSituations":"Custom text source returning a CharacterBufferReference over a null string; run sliced/copied incorrectly so its buffer reference was never initialized; interop path passing a null char buffer.","solutions":["Ensure the CharacterBufferReference passed in is constructed over a real buffer: new CharacterBufferReference(charArray, offset, count) or via TextStore","Fix the TextSource.GetTextRun implementation so the TextCharacters run itself was created with a valid buffer","Null-check characterBufferReference.CharacterBuffer before calling and rebuild the run if null","If the source string is empty/null, return a TextEndOfLine/empty run instead of a buffer reference over null"],"exampleFix":"// before\nvar reference = new CharacterBufferReference(null, 0); // null buffer\nrun.GetTextShapeableSymbols(props, reference, ...);\n// after\nif (text == null) text = string.Empty;\nvar reference = new CharacterBufferReference(text.ToCharArray(), 0);\nrun.GetTextShapeableSymbols(props, reference, ...);","handlingStrategy":"type-guard","validationCode":"if (characterBufferReference.CharacterBuffer == null)\n    throw new InvalidOperationException(\"Character buffer not initialized\");","typeGuard":"bool HasBuffer(CharacterBufferReference r) => r.CharacterBuffer != null;","tryCatchPattern":"try { run.GetTextShapeableSymbols(props, reference, ...); }\ncatch (ArgumentNullException ex) { log.Error(\"Null character buffer in run\", ex); throw; }","preventionTips":["Always construct CharacterBufferReference over an initialized string/array","Return an empty or TextEndOfLine run when source text is null","Verify run slicing code preserves the buffer reference"],"tags":["wpf","textformatting","null-argument","characterbuffer"],"backgroundTag":"null-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"}