{"record":{"id":"4a6faa99be13a5c6","repo":"EllanJiang/GameFramework","slug":"task-agent-is-invalid","errorCode":null,"errorMessage":"Task agent is invalid.","messagePattern":"Task agent is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Base/TaskPool/TaskPool.cs","lineNumber":130,"sourceCode":"        public void Shutdown()\n        {\n            RemoveAllTasks();\n\n            while (FreeAgentCount > 0)\n            {\n                m_FreeAgents.Pop().Shutdown();\n            }\n        }\n\n        /// <summary>\n        /// 增加任务代理。\n        /// </summary>\n        /// <param name=\"agent\">要增加的任务代理。</param>\n        public void AddAgent(ITaskAgent<T> agent)\n        {\n            if (agent == null)\n            {\n                throw new GameFrameworkException(\"Task agent is invalid.\");\n            }\n\n            agent.Initialize();\n            m_FreeAgents.Push(agent);\n        }\n\n        /// <summary>\n        /// 根据任务的序列编号获取任务的信息。\n        /// </summary>\n        /// <param name=\"serialId\">要获取信息的任务的序列编号。</param>\n        /// <returns>任务的信息。</returns>\n        public TaskInfo GetTaskInfo(int serialId)\n        {\n            foreach (ITaskAgent<T> workingAgent in m_WorkingAgents)\n            {\n                T workingTask = workingAgent.Task;\n                if (workingTask.SerialId == serialId)\n                {","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Base/TaskPool/TaskPool.cs#L112-L148","documentation":"TaskPool.AddAgent validates that the ITaskAgent<T> being registered is not null before pushing it onto the free-agent stack. A null agent would corrupt the pool's agent stacks and fail later during Update, so the pool fails fast at registration.","triggerScenarios":"Calling taskPool.AddAgent(null), typically when a helper/agent factory returned null (e.g. a custom ITaskAgent property never assigned, or GetComponent returned null in Unity).","commonSituations":"Setting up a download/websocket/asset task agent whose field wasn't initialized; refactoring where agent construction was moved behind a loader that hadn't run yet.","solutions":["Ensure the agent instance is created before calling AddAgent (new YourAgent(...) or GetComponent) and assert it is non-null","Check your agent factory/provider for silent null returns","Guard the call site: only AddAgent after successful initialization"],"exampleFix":"// before\nm_TaskPool.AddAgent(m_Agent); // m_Agent is null\n// after\nDebug.Assert(m_Agent != null, \"Task agent not initialized\");\nif (m_Agent != null) m_TaskPool.AddAgent(m_Agent);","handlingStrategy":"type-guard","validationCode":"if (agent == null) throw new ArgumentNullException(nameof(agent));\nm_TaskPool.AddAgent(agent);","typeGuard":"bool CanAdd(ITaskAgent<MyTask> agent) => agent != null;","tryCatchPattern":"try { taskPool.AddAgent(agent); }\ncatch (GameFrameworkException ex) { Debug.LogError($\"Agent registration failed: {ex.Message}\"); }","preventionTips":["Construct agents before pool registration","Assert helper fields are assigned in Awake","Check factories for silent null returns"],"tags":["taskpool","null-argument","unity","gameframework"],"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"}