{"record":{"id":"263eeaea385be22c","repo":"airbnb/epoxy","slug":"numitemstoprefetch-must-be-greater-than-0","errorCode":null,"errorMessage":"numItemsToPrefetch must be greater than 0","messagePattern":"numItemsToPrefetch must be greater than 0","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java","lineNumber":179,"sourceCode":"\n  /**\n   * @return The number of views to show on screen in this carousel at a time.\n   */\n  public float getNumViewsToShowOnScreen() {\n      return numViewsToShowOnScreen;\n  }\n\n  /**\n   * If you are using a Linear or Grid layout manager you can use this to set the item prefetch\n   * count. Only use this if you are not using {@link #setNumViewsToShowOnScreen(float)}\n   *\n   * @see #setNumViewsToShowOnScreen(float)\n   * @see LinearLayoutManager#setInitialPrefetchItemCount(int)\n   */\n  @ModelProp(group = \"prefetch\")\n  public void setInitialPrefetchItemCount(int numItemsToPrefetch) {\n    if (numItemsToPrefetch < 0) {\n      throw new IllegalStateException(\"numItemsToPrefetch must be greater than 0\");\n    }\n\n    // Use the linearlayoutmanager default of 2 if the user did not specify one\n    int prefetchCount = numItemsToPrefetch == 0 ? 2 : numItemsToPrefetch;\n\n    LayoutManager layoutManager = getLayoutManager();\n    if (layoutManager instanceof LinearLayoutManager) {\n      ((LinearLayoutManager) layoutManager).setInitialPrefetchItemCount(prefetchCount);\n    }\n  }\n\n  @Override\n  public void onChildAttachedToWindow(View child) {\n    if (numViewsToShowOnScreen > 0) {\n      ViewGroup.LayoutParams childLayoutParams = child.getLayoutParams();\n      child.setTag(R.id.epoxy_recycler_view_child_initial_size_id, childLayoutParams.width);\n\n      int itemSpacingPx = getSpacingDecorator().getPxBetweenItems();","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/airbnb/epoxy/blob/e45bd3a61fe3a1f130e184f5b8dcf172ab99025a/epoxy-adapter/src/main/java/com/airbnb/epoxy/Carousel.java#L161-L197","documentation":"Carousel.setInitialPrefetchItemCount configures LinearLayoutManager.setInitialPrefetchItemCount for nested-scrolling prefetching, which requires a positive count. Passing a negative value throws; note 0 is tolerated and treated as the default of 2.","triggerScenarios":"Calling carousel.setInitialPrefetchItemCount(n) with n < 0, e.g. via @ModelProp(group=\"prefetch\") binding in a controller with a negative computed value.","commonSituations":"Computing prefetch count from a variable that can go negative (e.g. itemsToPrefetch - shownItems), or misunderstanding that 0 means 'use default 2' and passing -1 intending 'disable prefetch'.","solutions":["Pass a value >= 0; use 0 to get the default prefetch count of 2.","Clamp the computed value: Math.max(0, computedCount).","If you intend to disable prefetch, the API does not support negative counts — use 0 (default) instead."],"exampleFix":"// before\ncarousel.setInitialPrefetchItemCount(-1); // throws\n\n// after\ncarousel.setInitialPrefetchItemCount(Math.max(0, computedCount));","handlingStrategy":"validation","validationCode":"if (prefetchCount < 0) prefetchCount = 0; // 0 means default of 2\ncarousel.setInitialPrefetchItemCount(prefetchCount);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed prefetch values with Math.max(0, n).","Remember 0 = default 2, negatives are illegal.","Sanitize values derived from arithmetic or config before passing."],"tags":["android","epoxy","carousel","prefetch","argument-validation"],"backgroundTag":"value-out-of-range","analyzedSha":"e45bd3a61fe3a1f130e184f5b8dcf172ab99025a","analyzedAt":"2026-09-13T03:24:16.052Z","contentChangedAt":"2026-09-13T03:24:16.052Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}