{"record":{"id":"6a4fa6011b4c9b6e","repo":"youth5201314/banner","slug":"getcontext-getstring-r-string-banner-adapter-nul","errorCode":null,"errorMessage":"getContext().getString(R.string.banner_adapter_null_error)","messagePattern":"getContext\\(\\)\\.getString\\(R\\.string\\.banner_adapter_null_error\\)","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"banner/src/main/java/com/youth/banner/Banner.java","lineNumber":708,"sourceCode":"    }\n\n    /**\n     * 移除一些引用\n     */\n    public void destroy() {\n        if (getViewPager2() != null && mPageChangeCallback != null) {\n            getViewPager2().unregisterOnPageChangeCallback(mPageChangeCallback);\n            mPageChangeCallback = null;\n        }\n        stop();\n    }\n\n    /**\n     * 设置banner的适配器\n     */\n    public Banner setAdapter(BA adapter) {\n        if (adapter == null) {\n            throw new NullPointerException(getContext().getString(R.string.banner_adapter_null_error));\n        }\n        this.mAdapter = adapter;\n        if (!isInfiniteLoop()) {\n            getAdapter().setIncreaseCount(0);\n        }\n        getAdapter().registerAdapterDataObserver(mAdapterDataObserver);\n        mViewPager2.setAdapter(adapter);\n        setCurrentItem(mStartPosition, false);\n        initIndicator();\n        return this;\n    }\n\n    /**\n     * 设置banner的适配器\n     * @param adapter\n     * @param isInfiniteLoop 是否支持无限循环\n     * @return\n     */","sourceCodeStart":690,"sourceCodeEnd":726,"githubUrl":"https://github.com/youth5201314/banner/blob/777c7dbfaa0a48d78101858d5520f695fef91fbd/banner/src/main/java/com/youth/banner/Banner.java#L690-L726","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nImageAdapter adapter = null;\nbanner.setAdapter(adapter); // NPE\n// after\nImageAdapter adapter = new ImageAdapter(imageUrls);\nbanner.setAdapter(adapter);","handlingStrategy":"validation","validationCode":"if (adapter == null) {\n    throw new IllegalArgumentException(\"Banner adapter must not be null\");\n}\nbanner.setAdapter(adapter);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["android","null-pointer","banner","adapter"],"backgroundTag":"null-argument","analyzedSha":"777c7dbfaa0a48d78101858d5520f695fef91fbd","analyzedAt":"2026-09-08T00:03:24.747Z","contentChangedAt":"2026-09-08T00:03:24.747Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}