From 8e29036de52e5108d5e9853abfcb5571b5643e86 Mon Sep 17 00:00:00 2001 From: Oleksandr Kelepko Date: Fri, 14 Dec 2018 15:56:42 -0800 Subject: [PATCH] Fix NPE in RecyclerEventsController Summary: Store mSectionsRecyclerView value in a local var to avoid it being set to null in `setSectionsRecyclerView(..)` between accesses. Reviewed By: paveldudka Differential Revision: D13469334 fbshipit-source-id: 76804c1496b769e580a34d4a6eb52e3fc3d68c40 --- .../widget/RecyclerEventsController.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/RecyclerEventsController.java b/litho-widget/src/main/java/com/facebook/litho/widget/RecyclerEventsController.java index 7e945800d72..e19259944dc 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/RecyclerEventsController.java +++ b/litho-widget/src/main/java/com/facebook/litho/widget/RecyclerEventsController.java @@ -39,8 +39,9 @@ public interface OnRecyclerUpdateListener { new Runnable() { @Override public void run() { - if (mSectionsRecyclerView != null && mSectionsRecyclerView.isRefreshing()) { - mSectionsRecyclerView.setRefreshing(false); + SectionsRecyclerView sectionsRecyclerView = mSectionsRecyclerView; + if (sectionsRecyclerView != null && sectionsRecyclerView.isRefreshing()) { + sectionsRecyclerView.setRefreshing(false); } } }; @@ -60,38 +61,41 @@ public void requestScrollToTop(boolean animated) { * @param animated if animated is set to true the scroll will happen with an animation. */ public void requestScrollToPosition(final int position, final boolean animated) { - if (mSectionsRecyclerView == null) { + SectionsRecyclerView sectionsRecyclerView = mSectionsRecyclerView; + if (sectionsRecyclerView == null) { return; } if (animated) { - mSectionsRecyclerView.getRecyclerView().smoothScrollToPosition(position); + sectionsRecyclerView.getRecyclerView().smoothScrollToPosition(position); return; } - mSectionsRecyclerView.getRecyclerView().scrollToPosition(position); + sectionsRecyclerView.getRecyclerView().scrollToPosition(position); } public void clearRefreshing() { - if (mSectionsRecyclerView == null || !mSectionsRecyclerView.isRefreshing()) { + SectionsRecyclerView sectionsRecyclerView = mSectionsRecyclerView; + if (sectionsRecyclerView == null || !sectionsRecyclerView.isRefreshing()) { return; } if (ThreadUtils.isMainThread()) { - mSectionsRecyclerView.setRefreshing(false); + sectionsRecyclerView.setRefreshing(false); return; } - mSectionsRecyclerView.removeCallbacks(mClearRefreshRunnable); - mSectionsRecyclerView.post(mClearRefreshRunnable); + sectionsRecyclerView.removeCallbacks(mClearRefreshRunnable); + sectionsRecyclerView.post(mClearRefreshRunnable); } public void showRefreshing() { - if (mSectionsRecyclerView == null || mSectionsRecyclerView.isRefreshing()) { + SectionsRecyclerView sectionsRecyclerView = mSectionsRecyclerView; + if (sectionsRecyclerView == null || sectionsRecyclerView.isRefreshing()) { return; } ThreadUtils.assertMainThread(); - mSectionsRecyclerView.setRefreshing(true); + sectionsRecyclerView.setRefreshing(true); } void setSectionsRecyclerView(@Nullable SectionsRecyclerView sectionsRecyclerView) { @@ -104,7 +108,8 @@ void setSectionsRecyclerView(@Nullable SectionsRecyclerView sectionsRecyclerView } public @Nullable RecyclerView getRecyclerView() { - return mSectionsRecyclerView == null ? null : mSectionsRecyclerView.getRecyclerView(); + SectionsRecyclerView sectionsRecyclerView = mSectionsRecyclerView; + return sectionsRecyclerView == null ? null : sectionsRecyclerView.getRecyclerView(); } public void setOnRecyclerUpdateListener(