{"record":{"id":"09e469539f22c8a3","repo":"dotnet/wpf","slug":"new-nullreferenceexception","errorCode":null,"errorMessage":"new NullReferenceException()","messagePattern":"new NullReferenceException\\(\\)","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs","lineNumber":215,"sourceCode":"\n        #endregion\n\n        #region Public Methods\n\n        /// <summary>\n        /// Returns a memory stream to the color profile bits\n        /// </summary>\n        public Stream OpenProfileStream()\n        {\n            //\n            // 3.* backwards compat for a \"bad\" ColorContext. Now the helper is a\n            // struct so when it's invalid we'll pretend it's a reference type. This\n            // should only happen if the color profile is corrupt (see early exits in\n            // ColorContext(SafeMILHandle) and FromRawBytes()).\n            //\n            if (_colorContextHelper.IsInvalid)\n            {\n                throw new NullReferenceException();\n            }\n            \n            uint profileSize = 0;\n            _colorContextHelper.GetColorProfileFromHandle(null, ref profileSize);\n            \n            byte[] profile = new byte[profileSize];\n            _colorContextHelper.GetColorProfileFromHandle(profile, ref profileSize);\n\n            return new MemoryStream(profile);\n        }\n\n        #endregion Public Methods\n\n        #region Public Properties\n\n        /// <summary>\n        /// ProfileUri\n        /// </summary>","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs#L197-L233","documentation":"OpenProfileStream throws NullReferenceException when the internal native color-context handle (_colorContextHelper) is invalid. Per the source comment, this simulates 3.x behavior: the context was constructed from a corrupt or unreadable profile (bad handle from ColorContext(SafeMILHandle) or FromRawBytes), so it behaves like a null object.","triggerScenarios":"Calling colorContext.OpenProfileStream() on a ColorContext created via ColorContext(SafeMILHandle) or FromRawBytes from corrupt/truncated ICC profile bytes, where the early-exit paths left an invalid handle.","commonSituations":"Reading embedded profiles from damaged/corrupted images or XPS packages; parsing raw ICC bytes downloaded incompletely or truncated by a bad stream.","solutions":["Validate the source profile bytes/URI before constructing the ColorContext; ensure the image or package is not truncated.","Check that the profile data came from a trusted, complete source; re-acquire the image/file and rebuild the context.","Wrap OpenProfileStream in try/catch for NullReferenceException and fall back to constructing a fresh ColorContext from a known-good profile URI."],"exampleFix":"// before\nusing var s = suspiciousContext.OpenProfileStream(); // NullReferenceException if handle invalid\n// after\nStream s;\ntry { s = suspiciousContext.OpenProfileStream(); }\ncatch (NullReferenceException) { s = new ColorContext(new Uri(\"pack://application:,,,/Profiles/sRGB.icc\", UriKind.Absolute)).OpenProfileStream(); }","handlingStrategy":"try-catch","validationCode":"if (context == null || context.ProfileUri == null && context.ToString() == string.Empty) { /* treat as invalid; rebuild from known-good profile */ }","typeGuard":"bool IsUsable(ColorContext c) { try { using var _ = c.OpenProfileStream(); return true; } catch (NullReferenceException) { return false; } }","tryCatchPattern":"try { using var s = ctx.OpenProfileStream(); Process(s); }\ncatch (NullReferenceException) { Log.Warning(\"Corrupt embedded color profile\"); RebuildContextFromDefaultProfile(); }","preventionTips":["Validate ICC profile bytes (128-byte header, size field) before FromRawBytes","Verify image/XPS integrity before extracting embedded profiles","Cache profiles from trusted sources instead of re-reading corrupt media"],"tags":["wpf","color-context","corrupt-profile","null-reference"],"backgroundTag":"file-read-failed","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"}