title: Recycler View nullpointer exception on scroll
body: <p>I have a custom texture view. and I have a recycler view, each cell in the recycler view has the texture view that I mentioned above. What I want to achieve is to play a video in the texture  view.Only the top visible texture view should start playing. However the code that I have written cast a null pointer exception when the first item is completely invisible and I am not touching the display. My code is as shown:</p>
  
  <pre><code>mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
              super.onScrollStateChanged(recyclerView, newState);
  
              mPrevTopItemPos = mCurrentTopItemPos;
              mCurrentTopItemPos = mLayoutManager.findFirstVisibleItemPosition();
  
  
  
              if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                  VSUtility.log("ScrollSTate:", "Idle");
                  ViewGroup parent = (ViewGroup) mRecyclerView.getChildAt(mCurrentTopItemPos);
                  FrameLayout videoFrame = (FrameLayout) parent.findViewById(R.id.surface_container);
  
                  if (videoFrame != null &amp;&amp; VSUtility.isViewVisible(videoFrame, mRecyclerView)) {
                      VSUtility.displayToastMessage(getActivity(), "frame  visible :" + mCurrentTopItemPos);
                  } else {
                      VSUtility.log("INVISIBLE:", "Idle" + mCurrentTopItemPos);
                  }
              }
          }
  
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              super.onScrolled(recyclerView, dx, dy);
              paginateIfNecessary(recyclerView);
  
  
              /*int itemAtTop = -1;
              Rect visibleRect = new Rect();
              itemAtTop = mLayoutManager.findFirstVisibleItemPosition();
              mPrevItemInList = itemAtTop;
              *//*VSCustomNearbyItemAdapter.ViewHolder holder = (VSCustomNearbyItemAdapter.ViewHolder)
                      mRecyclerView.getChildViewHolder(mRecyclerView.getChildAt(itemAtTop));*//*
              FrameLayout frameLayout = (FrameLayout) mRecyclerView.getChildAt(itemAtTop)
                      .findViewById(R.id.surface_container);
              //textureView.getWindowVisibleDisplayFrame(visibleRect);
              if (isViewVisible(frameLayout)) {
                  if (mPrevItemInList &gt; -1) {
  
                  }
                  SurfaceView sView = (SurfaceView) frameLayout.findViewById(R.id.surface_video_feeds);
                  ProgressBar progress = (ProgressBar) frameLayout.findViewById(R.id.progress_circular_video);
                  progress.setVisibility(View.VISIBLE);
  
                  VSUtility.log("Visiblity", "Visible");
              } else {
                  VSUtility.log("Visiblity", "Invisible");
              }*/
  
          }
      });
  </code></pre>
  
  <p>The error log is:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.findViewById(int)' on a null object reference
          at fragments.VSNearbyFragment$1.onScrollStateChanged(VSNearbyFragment.java:184)
          at android.support.v7.widget.RecyclerView.setScrollState(RecyclerView.java:823)
          at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:1704)
          at android.view.View.dispatchTouchEvent(View.java:8388)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
          at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
          at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
          at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
          at android.view.View.dispatchPointerEvent(View.java:8578)
          at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
          at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
          at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
          at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
          at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
          at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
          at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
          at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
          at android.view.InputEventReceiver.dispatchInputEv
  </code></pre>
  
  <p>The crash happens at the line :</p>
  
  <pre><code>   FrameLayout videoFrame = (FrameLayout) parent.findViewById(R.id.surface_container);
  </code></pre>
  
  <p>Thanks in advance.</p>