{"record":{"id":"8361fb5045061616","repo":"dotnet/wpf","slug":"argumentnullexception-contentarray","errorCode":null,"errorMessage":"ArgumentNullException: contentArray","messagePattern":"ArgumentNullException: contentArray","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs","lineNumber":292,"sourceCode":"                File.Delete(fileName);\n            }\n        }\n\n        //\n        // Save content stream for the given destination file\n        // UTF8 BOM should not be added to the contentArray.  This\n        // method adds BOM before writing file to disk.\n        //\n        public void WriteFile(byte[] contentArray, string destinationFile)\n        {\n            if (string.IsNullOrEmpty(destinationFile))\n            {\n                throw new ArgumentNullException(nameof(destinationFile));\n            }\n\n            if (contentArray == null)\n            {\n                throw new ArgumentNullException(nameof(contentArray));\n            }\n\n            UTF8Encoding utf8Encoding = new UTF8Encoding();\n            string contentStr = utf8Encoding.GetString(contentArray);\n\n            if (IsFileInHostManager(destinationFile))\n            {\n                // PutGeneratedFileContents adds BOM to the file when saving\n                // to memory or disk.\n                HostFileManager.PutGeneratedFileContents(destinationFile, contentStr);\n            }\n            else\n            {\n                // Add BOM for UTF8Encoding since the input contentArray is not supposed to\n                // have it already.\n                using (StreamWriter sw = new StreamWriter(destinationFile, false, new UTF8Encoding(true)))\n                {\n                    sw.WriteLine(contentStr);","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs#L274-L310","documentation":"TaskFileService.WriteFile requires a non-null content byte array; it decodes the array as UTF-8 and writes it (with BOM) to the destination or the host file manager. A null contentArray means there is nothing to write, so ArgumentNullException is thrown.","triggerScenarios":"Calling WriteFile(byte[] contentArray, string destinationFile) with contentArray == null.","commonSituations":"Upstream code generation or markup compilation produced no output (e.g. a generator returned null), and the caller passed the null buffer straight through to WriteFile.","solutions":["Check the content-producing step; fix the null source so generation always returns a buffer.","Guard the call site: skip the write or throw a descriptive error when content is null.","If content may legitimately be empty, pass Array.Empty<byte>() instead of null."],"exampleFix":"// before\nfileService.WriteFile(generatedContent, path);\n// after\nbyte[] generatedContent = GenerateContent();\nif (generatedContent == null) generatedContent = Array.Empty<byte>();\nfileService.WriteFile(generatedContent, path);","handlingStrategy":"validation","validationCode":"if (contentArray == null)\n    throw new ArgumentException(\"contentArray must not be null\", nameof(contentArray));","typeGuard":"bool HasContent(byte[] b) => b != null;","tryCatchPattern":"try { fileService.WriteFile(content, path); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"contentArray\") { log.Error(\"Generator returned null content\"); throw; }","preventionTips":["Make generators return Array.Empty<byte>() instead of null","Assert non-null output right after generation","Cover generation failure paths in tests"],"tags":["null-argument","file-write","content-generation"],"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"}