{"record":{"id":"00794cc0390ba716","repo":"Unity-Technologies/UnityCsReference","slug":"invalid-treeviewstate-it-is-null","errorCode":null,"errorMessage":"Invalid TreeViewState: it is null","messagePattern":"Invalid TreeViewState: it is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"critical","filePath":"Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs","lineNumber":66,"sourceCode":"\n        OverriddenMethods m_OverriddenMethods;\n        protected DoFoldoutCallback foldoutOverride { get; set; }\n\n        public TreeView(TreeViewState<TIdentifier> state)\n        {\n            Init(state);\n        }\n\n        public TreeView(TreeViewState<TIdentifier> state, MultiColumnHeader multiColumnHeader)\n        {\n            m_MultiColumnHeader = multiColumnHeader;\n            Init(state);\n        }\n\n        void Init(TreeViewState<TIdentifier> state)\n        {\n            if (state == null)\n                throw new ArgumentNullException(\"state\", \"Invalid TreeViewState: it is null\");\n\n            m_TreeView = new TreeViewController<TIdentifier>(null, state);\n            m_DataSource = new TreeViewControlDataSource(m_TreeView, this);\n            m_GUI = new TreeViewControlGUI(m_TreeView, this);\n            m_Dragging = new TreeViewControlDragging(m_TreeView, this);\n            m_TreeView.Init(new Rect(), m_DataSource, m_GUI, m_Dragging);\n\n            m_TreeView.searchChanged += SearchChanged;\n            m_TreeView.selectionChangedCallback += SelectionChanged;\n            m_TreeView.itemSingleClickedCallback += SingleClickedItem;\n            m_TreeView.itemDoubleClickedCallback += DoubleClickedItem;\n            m_TreeView.contextClickItemCallback += ContextClickedItem;\n            m_TreeView.contextClickOutsideItemsCallback += ContextClicked;\n            m_TreeView.expandedStateChanged += ExpandedStateChanged;\n            m_TreeView.keyboardInputCallback += KeyEvent;\n\n            m_TreeViewKeyControlID = GUIUtility.GetPermanentControlID();\n        }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GUI/TreeView/TreeViewControl/TreeViewControl.cs#L48-L84","documentation":"Thrown by the TreeView<TIdentifier> constructor path (via Init) when the provided TreeViewState is null. The TreeViewState carries selection, expansion, and scroll position; without it the TreeView cannot persist or render its state, so construction is rejected immediately.","triggerScenarios":"Constructing new TreeView(state) or new TreeView(state, multiColumnHeader) where state is null; deserializing a TreeViewState that failed to load from editor prefs and returned null.","commonSituations":"Creating a TreeView in an editor window without first creating/restoring the TreeViewState; passing a field that is initialized in OnEnable but the TreeView is constructed earlier; reload of a domain where the state was not serialized.","solutions":["Create a new TreeViewState before constructing the TreeView: var state = new TreeViewState();","When restoring, check for null and create a fresh state: state ?? new TreeViewState().","Ensure the state field is assigned in OnEnable before any TreeView construction."],"exampleFix":"// before\nvar tree = new TreeView<MyId>(m_State); // m_State is null\n\n// after\nm_State = m_State ?? new TreeViewState<MyId>();\nvar tree = new TreeView<MyId>(m_State);","handlingStrategy":"validation","validationCode":"m_State = m_State ?? new TreeViewState();\nvar tree = new TreeView<int>(m_State);","typeGuard":"static bool IsValidState(TreeViewState s) => s != null;","tryCatchPattern":null,"preventionTips":["Create the TreeViewState in OnEnable before constructing the TreeView.","Use null-coalescing (??) when restoring serialized state.","Never construct a TreeView with an uninitialized state field."],"tags":["unity","treeview","null-argument","construction","state"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}