{"record":{"id":"e301858ad965a745","repo":"EllanJiang/GameFramework","slug":"fsm-owner-is-invalid","errorCode":null,"errorMessage":"FSM owner is invalid.","messagePattern":"FSM owner is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Fsm/Fsm.cs","lineNumber":138,"sourceCode":"        {\n            get\n            {\n                return m_CurrentStateTime;\n            }\n        }\n\n        /// <summary>\n        /// 创建有限状态机。\n        /// </summary>\n        /// <param name=\"name\">有限状态机名称。</param>\n        /// <param name=\"owner\">有限状态机持有者。</param>\n        /// <param name=\"states\">有限状态机状态集合。</param>\n        /// <returns>创建的有限状态机。</returns>\n        public static Fsm<T> Create(string name, T owner, params FsmState<T>[] states)\n        {\n            if (owner == null)\n            {\n                throw new GameFrameworkException(\"FSM owner is invalid.\");\n            }\n\n            if (states == null || states.Length < 1)\n            {\n                throw new GameFrameworkException(\"FSM states is invalid.\");\n            }\n\n            Fsm<T> fsm = ReferencePool.Acquire<Fsm<T>>();\n            fsm.Name = name;\n            fsm.m_Owner = owner;\n            fsm.m_IsDestroyed = false;\n            foreach (FsmState<T> state in states)\n            {\n                if (state == null)\n                {\n                    throw new GameFrameworkException(\"FSM states is invalid.\");\n                }\n","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Fsm/Fsm.cs#L120-L156","documentation":"Fsm<T>.Create(string, T, params FsmState<T>[]) builds a new finite state machine bound to an owner object. The owner is mandatory because the FSM forwards it to every state's callbacks. A null owner fails the guard at Fsm.cs:138 and throws this exception before any state is initialized.","triggerScenarios":"Calling Fsm<T>.Create(name, null, state1, state2, ...) with a null owner via the params-array overload.","commonSituations":"Creating an FSM inside a component's constructor or field initializer before the owner instance exists; passing a null after a failed dependency-injection lookup; using Create in a static context where the owner is not yet assigned.","solutions":["Pass the actual owner instance (usually 'this' from the component that owns the FSM).","Ensure the owner object is constructed/initialized before creating the FSM (e.g. create the FSM in Start/OnInit rather than a field initializer)."],"exampleFix":"// before\nm_Fsm = Fsm<MyComponent>.Create(\"MyFsm\", null, new StateA(), new StateB());\n// after\nm_Fsm = Fsm<MyComponent>.Create(\"MyFsm\", this, new StateA(), new StateB());","handlingStrategy":"validation","validationCode":"if (owner == null) { throw new InvalidOperationException(\"FSM owner must be constructed before Create.\"); }\nvar fsm = Fsm<MyComponent>.Create(\"MyFsm\", owner, states);","typeGuard":"bool HasOwner<T>(T owner) where T : class => owner != null;","tryCatchPattern":"try { m_Fsm = Fsm<MyComponent>.Create(name, this, stateA, stateB); }\ncatch (GameFrameworkException ex) { Log.Error(\"FSM creation failed: \" + ex.Message); }","preventionTips":["Create the FSM in a lifecycle method (Start/OnInit) where 'this' is guaranteed valid, not in a field initializer.","Never pass null as owner — the FSM hands the owner to every state callback."],"tags":["csharp","fsm","null-argument"],"backgroundTag":"null-argument","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}