youth5201314/banner · error · NullPointerException
getContext().getString(R.string.banner_adapter_null_error)
Error message
getContext().getString(R.string.banner_adapter_null_error)
What it means
Banner.setAdapter() throws this NullPointerException when you pass a null adapter. The banner requires a non-null RecyclerView.Adapter to render its pages, so it fails fast with a localized message (R.string.banner_adapter_null_error) instead of crashing later inside the RecyclerView. It is an explicit guard in the library's public API.
Solutions
- Create and pass a concrete BannerAdapter/ImageAdapter instance: banner.setAdapter(new ImageAdapter(images)).
- Verify the adapter variable is non-null before calling setAdapter (assert or if-check).
- Ensure image/data list is initialized before building the adapter so the adapter itself isn't null.
- If an adapter is mandatory, never conditionally skip setAdapter; provide a default empty adapter instead.
Example fix
// before ImageAdapter adapter = null; banner.setAdapter(adapter); // NPE // after ImageAdapter adapter = new ImageAdapter(imageUrls); banner.setAdapter(adapter);
Defensive patterns
Strategy: validation
Validate before calling
if (adapter == null) {
throw new IllegalArgumentException("Banner adapter must not be null");
}
banner.setAdapter(adapter); Prevention
- Always instantiate the adapter immediately before setAdapter.
- Never build the adapter from nullable data without a null check.
- Keep banner setup code together so no setup step is accidentally skipped.
- Fail fast in your own factory methods instead of returning null adapters.
When it happens
Trigger: Calling banner.setAdapter(null) directly, or passing an adapter variable/factory method that returned null (e.g. an adapter not yet initialized, or a conditional adapter construction that fell through).
Common situations: Configuring the banner in an Activity/Fragment before the adapter is created; adapter built from data that is still null; copy-pasted setup code where the setAdapter line was commented out but a variable remained null; dependency-injection factory returning null on failure.
Related errors
AI-assisted analysis of youth5201314/banner@777c7dbfaa (2026-09-08).
Data as JSON: /api/errors/6a4fa6011b4c9b6e.
Report an issue: GitHub.
Appendix: source
Thrown at banner/src/main/java/com/youth/banner/Banner.java:708
}
/**
* 移除一些引用
*/
public void destroy() {
if (getViewPager2() != null && mPageChangeCallback != null) {
getViewPager2().unregisterOnPageChangeCallback(mPageChangeCallback);
mPageChangeCallback = null;
}
stop();
}
/**
* 设置banner的适配器
*/
public Banner setAdapter(BA adapter) {
if (adapter == null) {
throw new NullPointerException(getContext().getString(R.string.banner_adapter_null_error));
}
this.mAdapter = adapter;
if (!isInfiniteLoop()) {
getAdapter().setIncreaseCount(0);
}
getAdapter().registerAdapterDataObserver(mAdapterDataObserver);
mViewPager2.setAdapter(adapter);
setCurrentItem(mStartPosition, false);
initIndicator();
return this;
}
/**
* 设置banner的适配器
* @param adapter
* @param isInfiniteLoop 是否支持无限循环
* @return
*/View on GitHub (pinned to 777c7dbfaa)