0

How to fix an array out of bounds error - Out of Bounds Exception

  • You're confusing instantiation and adding. The list *is* initialized, but it doesn't have any items. If you don't understand the problem, imagining picking one item out of a pile of 0 items. The crash is in `holdItems`, at which point it likely hasn't received the data yet. – Zoe Mar 27 '19 at 17:15
  • Hi Zoe thank you for your response, yes apologies i meant adding instead of instantiation, i am aware that the data is not being received at holdItems, the issue i have is that i am unsure what i need to change in order for holdItems to receive the data from the database so that it populates the arraylist and does not throw the out of bounds exception. – Conor Crockford Mar 27 '19 at 17:28
  • I'm not familiar with Firebase, so I'm not entirely sure, but you likely have a thread issue. Comment the call to `holdItems` and check whether you get the items at all. If you don't, that's your main issue. If you do, it's a race condition between initialization and the `holdItems` call, in which case you need to delay the call – Zoe Mar 27 '19 at 17:30
  • Yes i can successfully get the items using a log, i tried that previous to posting the issue, i had previously tried moving code in order to resolve the race between initialization, this is where i am lost because i cant work out how to delay initialization. – Conor Crockford Mar 27 '19 at 17:40
  • Have you tried to move the logic in your `holdItems` method inside the listener, does it work that way? Please responde with @AlexMamo – Alex Mamo Mar 28 '19 at 11:11
  • @AlexMamo Hi Alex thank you for your response, I done some research today on how to delay the initialisation of my hold items and I have just been able to implement a timer task in order to delay holdItems from being called too early , so it now works fine :) – Conor Crockford Mar 28 '19 at 17:22
  • That's not a good pratice, check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Mar 28 '19 at 17:31

0 Answers0