Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value cannot be null.

Error message

Value cannot be null.

What it means

FileUtil.DeleteFileOrDirectory throws ArgumentNullException (whose default message is 'Value cannot be null.') when the path argument is the null reference, before it ever reaches the native delete. An empty string is handled separately with an ArgumentException. This is the standard null-guard on a managed-to-native file operation.

Source

Thrown at Editor/Mono/FileUtil.bindings.cs:22

using System;
using UnityEngine;
using UnityEngine.Bindings;
using System.IO;


namespace UnityEditor
{
    // Lets you do ''move'', ''copy'', ''delete'' operations over files or directories
    [NativeHeader("Runtime/Utilities/FileUtilities.h")]
    [NativeHeader("Runtime/Utilities/File.h")]
    [NativeHeader("Editor/Platform/Interface/EditorUtility.h")]
    public partial class FileUtil
    {
        // Deletes a file or a directory given a path.
        public static bool DeleteFileOrDirectory(string path)
        {
            if (path is null) throw new ArgumentNullException("path");
            if (path == string.Empty) throw new ArgumentException("path", "The path cannot be empty.");

            return DeleteFileOrDirectoryInternal(path);
        }

        [FreeFunction("DeleteFileOrDirectory")]
        private static extern bool DeleteFileOrDirectoryInternal(string path);

        [FreeFunction("IsPathCreated")]
        private static extern bool PathExists(string path);

        // Copies a file or directory.
        public static void CopyFileOrDirectory(string source, string dest)
        {
            CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);

            if (PathExists(dest))
            {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check the path before calling: if (!string.IsNullOrEmpty(path)) FileUtil.DeleteFileOrDirectory(path);.
  2. If the path comes from GUIDToAssetPath/Selection, validate the lookup returned a non-empty string first.
  3. Use the empty-string ArgumentException (separate path) to distinguish 'no path supplied' from 'path supplied but null'.

Example fix

// before
FileUtil.DeleteFileOrDirectory(maybePath);
// after
if (!string.IsNullOrEmpty(maybePath))
    FileUtil.DeleteFileOrDirectory(maybePath);
Defensive patterns

Strategy: validation

Validate before calling

if (!string.IsNullOrEmpty(path))
    FileUtil.DeleteFileOrDirectory(path);

Type guard

static bool IsDeletablePath(string p) => !string.IsNullOrEmpty(p);

Prevention

When it happens

Trigger: Calling DeleteFileOrDirectory(null) directly, or passing a variable that was never assigned (e.g. a missing AssetDatabase.GUIDToAssetPath result that returned null/empty before null-coalescing).

Common situations: Clean-up code that deletes a temp asset whose path lookup failed silently. Asset pipeline scripts deleting by a GUID that no longer exists. Build scripts that try to remove an optional output file that was never produced.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/2bfcd5aa853c5ee4. Report an issue: GitHub.