{"record":{"id":"b8f85e81967c8c84","repo":"Unity-Technologies/UnityCsReference","slug":"root","errorCode":null,"errorMessage":"root","messagePattern":"root","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GUI/Toolbars/EditorToolbar.cs","lineNumber":59,"sourceCode":"\n        public static OverlayToolbar CreateOverlay(IEnumerable<string> toolbarElementIds, EditorWindow context = null)\n        {\n            var root = new OverlayToolbar();\n\n            foreach (var id in toolbarElementIds)\n            {\n                if (TryCreateElement(id, context, out var ve))\n                    root.Add(ve);\n            }\n\n            return root;\n        }\n\n        // Used by MainToolbar, as it doesn't use the same Overlay styling\n        internal void LoadToolbarElements(VisualElement root)\n        {\n            if (root == null)\n                throw new ArgumentNullException(nameof(root));\n\n            EditorToolbarUtility.LoadStyleSheets(\"EditorToolbar\", root);\n\n            foreach (var id in m_ToolbarElements)\n            {\n                if(TryCreateElement(id, m_Context, out var ve))\n                    root.Add(ve);\n            }\n        }\n\n        static bool TryCreateElement(string id, EditorWindow ctx, out VisualElement ve)\n        {\n            if (EditorToolbarManager.instance.TryCreateElementFromId(ctx, id, out ve))\n            {\n                if (ve is IAccessContainerWindow visualWithContext)\n                    visualWithContext.containerWindow = ctx;\n                ve.AddToClassList(elementClassName);\n                return true;","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/Toolbars/EditorToolbar.cs#L41-L77","documentation":"Thrown by EditorToolbar.LoadToolbarElements when the root VisualElement argument is null. The method must attach style sheets and append toolbar elements to this root, so a null root would cause a NullReferenceException deeper in UI Toolkit without this early guard.","triggerScenarios":"Calling LoadToolbarElements passing a null VisualElement; the owning EditorWindow's rootVisualElement not yet initialized at call time.","commonSituations":"Invoking toolbar load before the window's UI tree exists; a refactor that changed the caller to pass a not-yet-created element.","solutions":["Ensure the EditorWindow's rootVisualElement is created before calling LoadToolbarElements.","Pass a valid container VisualElement created with 'new VisualElement()' if a detached root is intended.","Defer the call until the window's CreateGUI / OnEnable has run."],"exampleFix":"// before\nLoadToolbarElements(null);\n\n// after\nvar root = new VisualElement { name = \"toolbar-root\" };\nLoadToolbarElements(root);","handlingStrategy":"validation","validationCode":"if (root == null) throw new ArgumentNullException(nameof(root)); // or create one: root ??= new VisualElement();","typeGuard":"static bool IsReady(VisualElement root) => root != null;","tryCatchPattern":null,"preventionTips":["Call LoadToolbarElements only after the EditorWindow rootVisualElement is created.","Pass a freshly created VisualElement for detached roots.","Initialize UI in OnEnable/CreateGUI, not in the constructor."],"tags":["unity-editor","ui-toolkit","null-check","argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}