{"record":{"id":"b88598aa2e231342","repo":"stride3d/stride","slug":"value-cannot-be-null-parameter-getchildrencountfunc","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'getChildrenCountFunc')","messagePattern":"Value cannot be null\\. \\(Parameter 'getChildrenCountFunc'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Extensions/DependencyObjectExtensions.cs","lineNumber":252,"sourceCode":"                // failed to find visual parent\n                return null;\n            }\n        }\n\n        /// <summary>\n        /// Find the first child that match the given type.\n        /// </summary>\n        /// <typeparam name=\"T\">Type of child to find.</typeparam>\n        /// <param name=\"source\">Base node from where to start looking for child.</param>\n        /// <param name=\"getChildrenCountFunc\">Function that provide the number of children in the current element.</param>\n        /// <param name=\"getChildFunc\">Function that provide a given child element by its index.</param>\n        /// <returns>Returns the retrieved child, or null otherwise.</returns>\n        [CanBeNull]\n        private static T FindChildOfType<T>([NotNull] DependencyObject source, [NotNull] Func<DependencyObject, int> getChildrenCountFunc,\n            [NotNull] Func<DependencyObject, int, DependencyObject> getChildFunc) where T : DependencyObject\n        {\n            if (source == null) throw new ArgumentNullException(nameof(source));\n            if (getChildrenCountFunc == null) throw new ArgumentNullException(nameof(getChildrenCountFunc));\n            if (getChildFunc == null) throw new ArgumentNullException(nameof(getChildFunc));\n\n            var childCount = getChildrenCountFunc(source);\n            for (var i = 0; i < childCount; i++)\n            {\n                var child = getChildFunc(source, i);\n                if (child != null)\n                {\n                    if (child is T)\n                        return child as T;\n                    child = FindChildOfType<T>(child, getChildrenCountFunc, getChildFunc);\n                    if (child != null)\n                        return (T)child;\n                }\n            }\n            return null;\n        }\n","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Extensions/DependencyObjectExtensions.cs#L234-L270","documentation":"FindChildOfType<T> is a private helper in Stride's Wpf presentation library that walks a visual/logical tree using caller-supplied delegate functions. It validates all arguments up front and throws ArgumentNullException when the getChildrenCountFunc delegate is null, because the recursion cannot enumerate children without it.","triggerScenarios":"Calling FindChildOfType (or its public wrappers FindVisualChildOfType/FindLogicalChildOfType-style overloads) with a null children-count selector delegate while source and getChildFunc are valid.","commonSituations":"Constructing the delegates dynamically (e.g. from a reflected or conditional expression) so one branch yields null; refactoring call sites where the count delegate was removed but still passed; copy-pasting an overload call with wrong argument order.","solutions":["Pass a non-null count delegate, e.g. VisualTreeHelper.GetChildrenCount, when calling FindChildOfType.","Check argument order in the call — the count func is the 2nd parameter after source.","If the delegate is computed at runtime, throw or substitute a default (d => 0) instead of passing null.","Null-check the delegate at the call site before invoking the helper."],"exampleFix":"// before\nvar child = DependencyObjectExtensions.FindChildOfType<Button>(root, null, VisualTreeHelper.GetChild);\n// after\nvar child = DependencyObjectExtensions.FindChildOfType<Button>(root, VisualTreeHelper.GetChildrenCount, VisualTreeHelper.GetChild);","handlingStrategy":"validation","validationCode":"if (source == null) throw new InvalidOperationException(\"source is null\");\nif (getChildrenCountFunc == null) throw new InvalidOperationException(\"getChildrenCountFunc is null\");\nif (getChildFunc == null) throw new InvalidOperationException(\"getChildFunc is null\");","typeGuard":"static bool CanFindChild<T>(DependencyObject src, Func<DependencyObject,int> count, Func<DependencyObject,int,DependencyObject> get) where T : DependencyObject => src != null && count != null && get != null;","tryCatchPattern":"try { return FindChildOfType<Button>(root, count, get); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"getChildrenCountFunc\") { return null; }","preventionTips":["Always pass VisualTreeHelper.GetChildrenCount / GetChild (or logical equivalents) as the delegates","Check argument order in the 3-argument call","Fail fast with Debug.Assert on delegates before calling"],"tags":["wpf","argumentnull","visual-tree"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}