{"record":{"id":"2f1070482d9211a2","repo":"dotnet/maui","slug":"iitemsviewsource-is-empty","errorCode":null,"errorMessage":"IItemsViewSource is empty","messagePattern":"IItemsViewSource is empty","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/Android/CollectionView/EmptySource.cs","lineNumber":44,"sourceCode":"\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tif (HasHeader)\n\t\t\t{\n\t\t\t\treturn index == 1;\n\t\t\t}\n\n\t\t\treturn index == 0;\n\t\t}\n\n\t\tpublic int GetPosition(object item)\n\t\t{\n\t\t\treturn -1;\n\t\t}\n\n\t\tpublic object GetItem(int position)\n\t\t{\n\t\t\tthrow new IndexOutOfRangeException(\"IItemsViewSource is empty\");\n\t\t}\n\t}\n}","sourceCodeStart":26,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/Android/CollectionView/EmptySource.cs#L26-L47","documentation":"Thrown by EmptySource.GetItem(int) unconditionally. EmptySource is a sentinel IItemsViewSource representing an empty collection (Count == 0); calling GetItem on it is always invalid because there are no items. This throw indicates that the adapter or a caller failed to check Count before requesting an item, treating the empty sentinel as if it held data.","triggerScenarios":"A CollectionView or CarouselView is bound to a null or empty ItemsSource; the framework substitutes EmptySource. An internal code path (adapter, layout manager, or scroll helper) calls GetItem(0) without first verifying Count > 0. This is typically a framework-side bounds-check bug rather than a direct caller mistake.","commonSituations":"CollectionView/CarouselView with ItemsSource initially null, where a scroll-to or position operation races ahead of data binding. Framework version regressions where an adapter GetItem call lost its Count guard. Triggering a programmatic ScrollTo before the source is populated.","solutions":["Ensure ItemsSource is non-null and populated before issuing programmatic ScrollTo or Position commands.","Avoid calling ScrollTo(position) when the collection is known to be empty; guard with a count check.","Upgrade the Maui/Forms version; this throw often signals an internal bounds-check regression that gets patched.","If reproducing reliably, report it with the adapter call stack so the missing Count guard upstream can be fixed."],"exampleFix":"// before\ncollectionView.ScrollTo(0, position: ScrollToPosition.Start); // may throw if source empty\n\n// after\nif (collectionView.ItemsSource is IList list && list.Count > 0)\n    collectionView.ScrollTo(0, position: ScrollToPosition.Start);","handlingStrategy":"validation","validationCode":"// Guard ScrollTo/GetItem callers with a count check.\nstatic bool HasItems(IItemsViewSource source) => source != null && source.Count > 0;\n// Usage:\nif (collectionView.ItemsSource is IList list && list.Count > 0)\n    collectionView.ScrollTo(0);","typeGuard":null,"tryCatchPattern":"try\n{\n    collectionView.ScrollTo(index);\n}\ncatch (IndexOutOfRangeException ex) when (ex.Message.Contains(\"IItemsViewSource is empty\"))\n{\n    // Source is empty; no-op or defer the scroll until data arrives.\n    Log.Debug(nameof(CollectionView), \"ScrollTo skipped; source is empty.\");\n}","preventionTips":["Ensure ItemsSource is non-null and populated before issuing programmatic ScrollTo calls.","Guard all adapter GetItem calls with a Count > 0 check.","Keep the Maui/Forms packages updated; this throw often reflects an internal bounds-check regression.","Report reproducible cases with the full adapter call stack to help fix the upstream guard."],"tags":["android","collectionview","emptysource","bounds-check","internal"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}