dotnet/wpf · error · ArgumentNullException

ArgumentNullException: contentArray

Error message

ArgumentNullException: contentArray

What it means

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.

Solutions

  1. Check the content-producing step; fix the null source so generation always returns a buffer.
  2. Guard the call site: skip the write or throw a descriptive error when content is null.
  3. If content may legitimately be empty, pass Array.Empty<byte>() instead of null.

Example fix

// before
fileService.WriteFile(generatedContent, path);
// after
byte[] generatedContent = GenerateContent();
if (generatedContent == null) generatedContent = Array.Empty<byte>();
fileService.WriteFile(generatedContent, path);
Defensive patterns

Strategy: validation

Validate before calling

if (contentArray == null)
    throw new ArgumentException("contentArray must not be null", nameof(contentArray));

Type guard

bool HasContent(byte[] b) => b != null;

Try / catch

try { fileService.WriteFile(content, path); }
catch (ArgumentNullException ex) when (ex.ParamName == "contentArray") { log.Error("Generator returned null content"); throw; }

Prevention

When it happens

Trigger: Calling WriteFile(byte[] contentArray, string destinationFile) with contentArray == null.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8361fb5045061616. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/MS/Internal/Tasks/TaskFileService.cs:292

                File.Delete(fileName);
            }
        }

        //
        // Save content stream for the given destination file
        // UTF8 BOM should not be added to the contentArray.  This
        // method adds BOM before writing file to disk.
        //
        public void WriteFile(byte[] contentArray, string destinationFile)
        {
            if (string.IsNullOrEmpty(destinationFile))
            {
                throw new ArgumentNullException(nameof(destinationFile));
            }

            if (contentArray == null)
            {
                throw new ArgumentNullException(nameof(contentArray));
            }

            UTF8Encoding utf8Encoding = new UTF8Encoding();
            string contentStr = utf8Encoding.GetString(contentArray);

            if (IsFileInHostManager(destinationFile))
            {
                // PutGeneratedFileContents adds BOM to the file when saving
                // to memory or disk.
                HostFileManager.PutGeneratedFileContents(destinationFile, contentStr);
            }
            else
            {
                // Add BOM for UTF8Encoding since the input contentArray is not supposed to
                // have it already.
                using (StreamWriter sw = new StreamWriter(destinationFile, false, new UTF8Encoding(true)))
                {
                    sw.WriteLine(contentStr);

View on GitHub (pinned to 81131a70a4)