{"record":{"id":"aab6a2423bf22db2","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-apply-or-revert-object-object-is-null","errorCode":null,"errorMessage":"Cannot apply or revert object. Object is null.","messagePattern":"Cannot apply or revert object\\. Object is null\\.","errorType":"exception","errorClass":"NullReferenceException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":247,"sourceCode":"                    {\n                        assetsToReload.Add(importer.assetPath);\n                    }\n                }\n            }\n\n            foreach (var path in assetsToReload)\n            {\n                AssetDatabase.WriteImportSettingsIfDirty(path);\n                AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);\n            }\n        }\n\n        static void ThrowExceptionIfNotValidPrefabInstanceObject(Object prefabInstanceObject, bool isApply)\n        {\n            if (!(prefabInstanceObject is GameObject || prefabInstanceObject is Component))\n                throw new ArgumentException(\"Calling apply or revert methods on an object which is not a GameObject or Component is not supported.\", nameof(prefabInstanceObject));\n            if (prefabInstanceObject == null)\n                throw new NullReferenceException(\"Cannot apply or revert object. Object is null.\");\n            if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstanceObject))\n                throw new ArgumentException(\"Calling apply or revert methods on an object which is not part of a Prefab instance is not supported.\", nameof(prefabInstanceObject));\n\n            // We support revert operations on Prefab Assets, but not apply operations.\n            if (isApply)\n                ThrowExceptionIfInstanceIsPersistent(prefabInstanceObject);\n        }\n\n        static void ThrowExceptionIfAllPrefabInstanceObjectsAreInvalid(Object[] prefabInstanceObjects, bool isApply)\n        {\n            foreach (var obj in prefabInstanceObjects)\n            {\n                if (obj != null && (obj is GameObject || obj is Component) && IsPartOfPrefabInstance(obj) && !(isApply && EditorUtility.IsPersistent(obj)))\n                    return;\n            }\n\n            // Throw exception if all objects are invalid\n            throw new ArgumentException(\"Cannot apply or revert on any of the objects. Attempt with individual objects for details.\", nameof(prefabInstanceObjects));","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L229-L265","documentation":"Declared in ThrowExceptionIfNotValidPrefabInstanceObject as a NullReferenceException for a null prefabInstanceObject. NOTE: because the type-check (`is GameObject/Component`) runs first and `null is X` is always false, a null input actually triggers error 555 first, so this specific NullReferenceException is effectively unreachable through this guard in normal flow. It documents intent: apply/revert must not receive null.","triggerScenarios":"Theoretically a null prefabInstanceObject; in practice unreachable because the preceding `is` guard rejects null as 'not a GameObject or Component'. Could surface only if the method ordering changed or a subclass made `is` return true for null (which C# does not).","commonSituations":"Effectively never hit given the current ordering; treat a null-related apply/revert failure as error 555. Included for completeness/defense.","solutions":["Ensure the object is non-null before calling apply/revert (this also prevents 555).","Treat any null-input apply/revert failure as error 555 and fix the type/null upstream.","Add an explicit null guard upstream so you get your own clear message, not Unity's."],"exampleFix":"// before\nPrefabUtility.ApplyPrefabOverride(null, path); // lands as 555 in practice\n\n// after\nif (obj == null) { Debug.LogWarning(\"Object is null; cannot apply.\"); return; }\nif (!(obj is GameObject || obj is Component)) return;\nPrefabUtility.ApplyPrefabOverride(obj, path);","handlingStrategy":"validation","validationCode":"if (prefabInstanceObject == null)\n{ Debug.LogWarning(\"Cannot apply/revert a null object.\"); return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add an explicit null guard upstream so you never rely on Unity's internal ordering.","Treat null-input apply/revert as error 555 in practice and fix the type/null source.","Validate object resolution (lookups, selections) for null before apply/revert."],"tags":["prefab","apply-revert","null-reference","unreachable"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}