{"record":{"id":"ba1f54592b85440e","repo":"stride3d/stride","slug":"value-cannot-be-null-parameter-getchildfunc","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'getChildFunc')","messagePattern":"Value cannot be null\\. \\(Parameter 'getChildFunc'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Extensions/DependencyObjectExtensions.cs","lineNumber":253,"sourceCode":"                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\n        /// <summary>","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Extensions/DependencyObjectExtensions.cs#L235-L271","documentation":"FindChildOfType<T> throws ArgumentNullException when the getChildFunc delegate is null. This delegate supplies each child by index during the tree walk; without it the search cannot proceed, so the method validates it eagerly at entry.","triggerScenarios":"Invoking FindChildOfType (directly or via a public wrapper) with a null child-accessor delegate while source and getChildrenCountFunc are non-null.","commonSituations":"Passing null for an optional-feeling parameter; mixing up the order of the two Func arguments (count func vs child func); a factory expression returning null on some code path.","solutions":["Pass a non-null child accessor such as VisualTreeHelper.GetChild or LogicalTreeHelper.GetChild.","Verify the two delegates are not swapped: count func is (DependencyObject,int)->int, child func is (DependencyObject,int,DependencyObject).","If delegates are built dynamically, assert non-null before calling.","Provide a fallback accessor instead of null when the tree helper is unavailable."],"exampleFix":"// before\nvar child = FindChildOfType<Button>(root, VisualTreeHelper.GetChildrenCount, null);\n// after\nvar child = FindChildOfType<Button>(root, VisualTreeHelper.GetChildrenCount, VisualTreeHelper.GetChild);","handlingStrategy":"validation","validationCode":"if (getChildFunc == null) throw new InvalidOperationException(\"getChildFunc is null\");","typeGuard":"static bool IsValidAccessors(Func<DependencyObject,int> count, Func<DependencyObject,int,DependencyObject> get) => count != null && get != null;","tryCatchPattern":"try { return FindChildOfType<Button>(root, count, get); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"getChildFunc\") { return null; }","preventionTips":["Keep the count and child delegates adjacent in the call and name them at the call site","Do not make delegate arguments optional/null","Unit-test tree-walking helpers with default VisualTreeHelper delegates"],"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"}