title: Getting out of bounds exception when paginating
body: <p>I implemented the solution provided by Alex Mamo here: <a href="https://www.youtube.com/watch?v=KdgKvLll07s&amp;t=118s" rel="nofollow noreferrer">https://www.youtube.com/watch?v=KdgKvLll07s&amp;t=118s</a>. However, I keep getting this error:</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
  
  </code></pre>
  
  <p>on this line, inside the onScrollListener:</p>
  
  <pre><code>lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
  </code></pre>
  
  <p>Here is the main snippet from the on createView method:</p>
  
  <pre><code>restaurantItemsArrayList = new ArrayList&lt;&gt;();
          restaurantRecycler = restaurantView.findViewById(R.id.restaurant_recycler);
          linearLayoutManager = new LinearLayoutManager(restaurantContext);
          restaurantRecycler.setHasFixedSize(true);
          restaurantRecycler.setLayoutManager(linearLayoutManager);
  
          restaurantQuery = FirebaseFirestore.getInstance().collection("Restaurant_Data").whereEqualTo("restaurantAddress", selectedCity).limit(LOAD_LIMIT);
          restaurantQuery.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
              @Override
              public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                  if (task.isSuccessful()) {
                      for (DocumentSnapshot document : task.getResult()) {
                          RestaurantItems restaurantItems = document.toObject(RestaurantItems.class);
                          restaurantItemsArrayList.add(restaurantItems);
                      }
                      restaurantAdapter = new RestaurantAdapter(restaurantContext, restaurantItemsArrayList);
                      restaurantRecycler.setAdapter(restaurantAdapter);
                      lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
  
                      restaurantAdapter.setRestaurantAdapterListener(new RestaurantAdapter.RestaurantAdapterListener() {
                          @Override
                          public void onRestaurantSend(String restaurantId, String menuPath) {
                              String path = menuPath+"/Menu_Card";
                              bundle.putString("restaurantId", restaurantId);
                              bundle.putString("restaurantPath", path);
                              bundle.putBoolean("direction", true);
                              restaurantSelectListener.onRestaurantSelect(bundle);
                          }
                      });
  
                      RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                          @Override
                          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                              super.onScrollStateChanged(recyclerView, newState);
                              if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                                  isScrolling = true;
                              }
                          }
  
                          @Override
                          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                              super.onScrolled(recyclerView, dx, dy);
  
                              LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
                              int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                              int visibleItemCount = linearLayoutManager.getChildCount();
                              int totalItemCount = linearLayoutManager.getItemCount();
  
                              if (isScrolling &amp;&amp; (firstVisibleItemPosition + visibleItemCount == totalItemCount) &amp;&amp; !isLastItem) {
                                  isScrolling = false;
  
                                  restaurantNextQuery = FirebaseFirestore.getInstance().collection("Restaurant_Data").whereEqualTo("restaurantAddress", selectedCity).startAfter(lastVisible).limit(LOAD_LIMIT);
                                  restaurantNextQuery.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                                      @Override
                                      public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                                          if (task.isSuccessful()) {
                                              for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                                  RestaurantItems restaurantItems = documentSnapshot.toObject(RestaurantItems.class);
                                                  restaurantItemsArrayList.add(restaurantItems);
                                              }
                                              restaurantAdapter.notifyDataSetChanged();
                                              lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
  
                                              restaurantAdapter.setRestaurantAdapterListener(new RestaurantAdapter.RestaurantAdapterListener() {
                                                  @Override
                                                  public void onRestaurantSend(String restaurantId, String menuPath) {
                                                      String path = menuPath+"/Menu_Card";
                                                      bundle.putString("restaurantId", restaurantId);
                                                      bundle.putString("restaurantPath", path);
                                                      bundle.putBoolean("direction", true);
                                                      restaurantSelectListener.onRestaurantSelect(bundle);
                                                  }
                                              });
  
                                              if (task.getResult().size() &lt; LOAD_LIMIT) {
                                                  isLastItem = true;
                                              }
                                          }
                                      }
                                  });
                              }
                          }
                      };
                      restaurantRecycler.addOnScrollListener(onScrollListener);
                  }
              }
          });
  
          backArrow.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  bundle.putBoolean("direction", false);
                  restaurantSelectListener.onRestaurantSelect(bundle);
              }
          });
          return restaurantView;
      }
  </code></pre>