{"record":{"id":"306d4724bcad7154","repo":"dotnet/wpf","slug":"sr-format-sr-argumentpropertymustnotbenull-uriresource","errorCode":null,"errorMessage":"SR.Format(SR.ArgumentPropertyMustNotBeNull, \"uriResource\", \"OriginalString\")","messagePattern":"SR\\.Format\\(SR\\.ArgumentPropertyMustNotBeNull, \"uriResource\", \"OriginalString\"\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":579,"sourceCode":"        ///\n        ///   Such as\n        ///    Resource from Application assembly\n        ///         \"image/picture1.jpg\" or\n        ///         \"pack://application:,,,/image/picture1.jpg\"\n        ///\n        ///    Resource from a library assembly\n        ///         \"mylibrary;component/image/picture2.png\" or\n        ///         \"pack://application:,,,/mylibrary;component/image/picture3.jpg\"\n        ///\n        /// </summary>\n        /// <param name=\"uriResource\">the uri maps to the resource</param>\n        /// <returns>PackagePart or null</returns>\n        public static StreamResourceInfo GetResourceStream(Uri uriResource)\n        {\n            ArgumentNullException.ThrowIfNull(uriResource);\n\n            if (uriResource.OriginalString == null)\n                throw new ArgumentException(SR.Format(SR.ArgumentPropertyMustNotBeNull, \"uriResource\", \"OriginalString\"));\n\n            if (uriResource.IsAbsoluteUri && !BaseUriHelper.IsPackApplicationUri(uriResource))\n            {\n                throw new ArgumentException(SR.NonPackAppAbsoluteUriNotAllowed);\n            }\n\n            ResourcePart part = GetResourceOrContentPart(uriResource) as ResourcePart;\n            return (part == null) ? null : new StreamResourceInfo(part.GetSeekableStream(), part.ContentType);\n        }\n\n        /// <summary>\n        /// Get PackagePart for a uri, the uri maps to a content file which is associated\n        /// with the application assembly.\n        ///\n        /// If the Uri doesn't map to any content file, this method returns null.\n        ///\n        /// The accepted uri could be relative uri or pack://Application:,,,/ uri.\n        ///","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L561-L597","documentation":"Application.GetResourceStream(Uri) validates uriResource and throws ArgumentException (SR.ArgumentPropertyMustNotBeNull naming uriResource.OriginalString) when the Uri's OriginalString is null. GetResourceStream returns a StreamResourceInfo for an application resource and relies on a well-formed relative or pack-application Uri.","triggerScenarios":"Calling GetResourceStream with a Uri instance whose OriginalString is null (e.g. from deserialization or an uninitialized property).","commonSituations":"Reading resource paths from settings that were never populated; a Uri produced by an external library with a null backing string.","solutions":["Create the Uri from a non-null string, e.g. new Uri(\"images/logo.png\", UriKind.Relative)","Guard the call: if (uri == null || uri.OriginalString == null) skip/throw a clearer error","Fix the source that produced the degenerate Uri"],"exampleFix":"// before\nUri uri = LoadFromConfig(); // OriginalString == null\nvar sri = Application.GetResourceStream(uri);\n\n// after\nvar uri = new Uri(\"images/logo.png\", UriKind.Relative);\nvar sri = Application.GetResourceStream(uri);","handlingStrategy":"validation","validationCode":"if (uri == null || uri.OriginalString == null)\n    return null; // or throw a clearer error\nvar sri = Application.GetResourceStream(uri);","typeGuard":"static bool IsValidResourceUri(Uri u) => u?.OriginalString != null;","tryCatchPattern":"try { sri = Application.GetResourceStream(uri); }\ncatch (ArgumentException ex) { Log(ex); sri = null; }","preventionTips":["Validate resource paths read from settings before wrapping in a Uri","Build Uris with UriKind.Relative from constant/validated strings","Handle null StreamResourceInfo return as well as argument errors"],"tags":["wpf","uri","argument-validation","resources"],"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"}