EllanJiang/GameFramework · error · GameFrameworkException

Condition is invalid.

Error message

Condition is invalid.

What it means

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.

Solutions

  1. Ensure the predicate passed to HasDataRow is non-null before calling, e.g. check `if (condition == null) throw/return`
  2. Assign a valid lambda or method group: `dataTable.HasDataRow(row => row.Id == targetId)`
  3. If the predicate is optional in your logic, branch: call the null-predicate path or a different overload instead of passing null
  4. Catch GameFrameworkException around the call if the condition comes from external input

Example fix

// before
Predicate<MyRow> pred = GetPredicate(); // may return null
bool exists = dataTable.HasDataRow(pred);
// after
Predicate<MyRow> pred = GetPredicate() ?? (row => row.Id > 0);
bool exists = dataTable.HasDataRow(pred);
Defensive patterns

Strategy: validation

Validate before calling

if (condition == null) throw new ArgumentException("condition must not be null", nameof(condition));
dataTable.HasDataRow(condition);

Type guard

static bool IsValid<T>(Predicate<T> p) => p != null;

Try / catch

try { exists = dataTable.HasDataRow(condition); }
catch (GameFrameworkException ex) when (ex.Message == "Condition is invalid.") { Log.Error("null condition passed to HasDataRow"); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/48dd1ffa3411971b. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/DataTable/DataTableManager.DataTable.cs:114

            /// 检查是否存在数据表行。
            /// </summary>
            /// <param name="id">数据表行的编号。</param>
            /// <returns>是否存在数据表行。</returns>
            public override bool HasDataRow(int id)
            {
                return m_DataSet.ContainsKey(id);
            }

            /// <summary>
            /// 检查是否存在数据表行。
            /// </summary>
            /// <param name="condition">要检查的条件。</param>
            /// <returns>是否存在数据表行。</returns>
            public bool HasDataRow(Predicate<T> condition)
            {
                if (condition == null)
                {
                    throw new GameFrameworkException("Condition is invalid.");
                }

                foreach (KeyValuePair<int, T> dataRow in m_DataSet)
                {
                    if (condition(dataRow.Value))
                    {
                        return true;
                    }
                }

                return false;
            }

            /// <summary>
            /// 获取数据表行。
            /// </summary>
            /// <param name="id">数据表行的编号。</param>
            /// <returns>数据表行。</returns>

View on GitHub (pinned to d0c010b051)