• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Firebase RecyclerView IndexOutOfBounds Exception

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening everyone,

Right now, I just want to add an element to an recyclerView adapter, but when I add the second item, the logcat on Android Studio shows the following exception:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:457)
at com.switchswan.FirebaseRecyclerAdapter$1.onChildAdded(FirebaseRecyclerAdapter.java:101)



The code adds the items to Firebase with no problems, but it appears that the recyclerView cannot reflect what is in the Firebase Database.
Screenshot-from-2018-01-13-21-48-55.png
[Thumbnail for Screenshot-from-2018-01-13-21-48-55.png]
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0  


That says the list was empty(size = 0) when an attempt was made to access at index of 1.

What line in the posted code corresponds to line 101 shown in the stack trace?
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 101 is "mItems.add(nextIndex, item);"
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 45 in this snippet of code
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The error message says that when that statement is executed the mItems list is empty and the value of nextIndex is 1.

For example:


Check the logic to see why nextIndex has a value of 1.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just realized that the mItems size is 0 but I added 1 item to the ArrayList. So why is the size still 0 for  mItems?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I added 1 item to the ArrayList. So why is the size still 0 for  mItems?


Are you sure you added anything to mItems?  Have you traced the code's execution to see why there is nothing there?
 
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that there's an off-by-one error caused by the +1 in line 43 of the posted code. However I don't see any connection between the things mentioned in that line of code and the mItems list, so I could be guessing in the dark.
 
Paul Clapham
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually... let's have a closer look at this code:



The rule for List's single-parameter add() method is that the element is added at the end of the list. So we can look at this code like this:



Now we're assuming that mItems and mKeys always have the same number of elements, I think, so given the comparison being made in the first line we can look at the code like this:



So now it's clear that the original code was too complicated and that it can be replaced by this code:



And it also becomes apparent that getting the value of nextIndex correct is of critical importance.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After adding the first Item that ArrayList holding items goes back to 0 when I add the second item.
 
Elisha Olade
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FirebaseRecyclerAdapter.java

Class.java

MainActivity.java

 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ArrayList holding items goes back to 0 when I add the second item.


How did you detect that?
Did you show the array's length just before the add of the second item and it was not 0
and then after the add it's length was 1?

Add some print statements that show the length of the array list every time something is assigned to the  variable mItems and every time something is added to it
The print outs will show in the logcat.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic