{"record":{"id":"48dd1ffa3411971b","repo":"EllanJiang/GameFramework","slug":"condition-is-invalid","errorCode":null,"errorMessage":"Condition is invalid.","messagePattern":"Condition is invalid\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/DataTable/DataTableManager.DataTable.cs","lineNumber":114,"sourceCode":"            /// 检查是否存在数据表行。\n            /// </summary>\n            /// <param name=\"id\">数据表行的编号。</param>\n            /// <returns>是否存在数据表行。</returns>\n            public override bool HasDataRow(int id)\n            {\n                return m_DataSet.ContainsKey(id);\n            }\n\n            /// <summary>\n            /// 检查是否存在数据表行。\n            /// </summary>\n            /// <param name=\"condition\">要检查的条件。</param>\n            /// <returns>是否存在数据表行。</returns>\n            public bool HasDataRow(Predicate<T> condition)\n            {\n                if (condition == null)\n                {\n                    throw new GameFrameworkException(\"Condition is invalid.\");\n                }\n\n                foreach (KeyValuePair<int, T> dataRow in m_DataSet)\n                {\n                    if (condition(dataRow.Value))\n                    {\n                        return true;\n                    }\n                }\n\n                return false;\n            }\n\n            /// <summary>\n            /// 获取数据表行。\n            /// </summary>\n            /// <param name=\"id\">数据表行的编号。</param>\n            /// <returns>数据表行。</returns>","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/DataTable/DataTableManager.DataTable.cs#L96-L132","documentation":"DataTableManager.DataTable<T>.HasDataRow validates its required Predicate<T> delegate before iterating the row set. If the caller passes a null predicate, the library throws GameFrameworkException(\"Condition is invalid.\") instead of failing later inside the loop with a NullReferenceException.","triggerScenarios":"Calling HasDataRow(condition) with a Predicate<T> that is null — e.g. an uninitialized delegate field, a method that conditionally assigns the predicate, or passing null from an untyped/dynamic caller.","commonSituations":"A search/filter delegate stored in a nullable field that was never assigned; a refactored call site where the lambda was moved out and the variable defaults to null; passing a helper method group that failed to resolve to null under an #if block.","solutions":["Ensure the predicate passed to HasDataRow is non-null before calling, e.g. check `if (condition == null) throw/return`","Assign a valid lambda or method group: `dataTable.HasDataRow(row => row.Id == targetId)`","If the predicate is optional in your logic, branch: call the null-predicate path or a different overload instead of passing null","Catch GameFrameworkException around the call if the condition comes from external input"],"exampleFix":"// before\nPredicate<MyRow> pred = GetPredicate(); // may return null\nbool exists = dataTable.HasDataRow(pred);\n// after\nPredicate<MyRow> pred = GetPredicate() ?? (row => row.Id > 0);\nbool exists = dataTable.HasDataRow(pred);","handlingStrategy":"validation","validationCode":"if (condition == null) throw new ArgumentException(\"condition must not be null\", nameof(condition));\ndataTable.HasDataRow(condition);","typeGuard":"static bool IsValid<T>(Predicate<T> p) => p != null;","tryCatchPattern":"try { exists = dataTable.HasDataRow(condition); }\ncatch (GameFrameworkException ex) when (ex.Message == \"Condition is invalid.\") { Log.Error(\"null condition passed to HasDataRow\"); }","preventionTips":["Never store predicates in unassigned nullable fields; initialize with row => true","Null-check delegates at API boundaries before forwarding into DataTable calls","Prefer inline lambdas over mutable delegate variables"],"tags":["csharp","datatable","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-16T04:17:20.429Z"}