NativeExpressAdView java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

405 views
Skip to first unread message

Renshi Ramon

unread,
May 29, 2017, 6:29:15 AM5/29/17
to Google Mobile Ads SDK Developers
Hello Everyone,
I am working on android app which fetch data using
RecyclerView and Retrofit for parsing JSON Url
I had following  tutorial on this Github

My project on Android Studio has no Errors and I'm able to run the Application. But when I open the MainActivity it crashes.

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

This is the following line is on setUpAndLoadNativeExpressAds

final NativeExpressAdView adView = (NativeExpressAdView) mRecyclerViewItems.get(i);

Thanks in advance!

KontenItem.java
MainActivity.java
KontenAdapter.java

Ivan Bautista (Mobile Ads SDK Team)

unread,
May 29, 2017, 11:53:41 AM5/29/17
to Google Mobile Ads SDK Developers
Hi Renshi,

As the error states, you are trying to get an item at index 0 in an ArrayList (mRecyclerViewItems) that has no elements (size is 0). Based from setUpAndLoadNativeExpressAds() method, you shouldn't traverse through a list that is empty especially if you're going to extract elements from the list inside the loop as this will cause the exception. 

Second, in your MainActivity.java, you are calling setUpAndLoadNativeExpressAds() without checking whether the JSON data needed to fill the mRecyclerViewItems list has returned successfully. This resulted to having an empty mRecyclerViewItems list and caused the IndexOutOfBoundsException. Make sure to call setUpAndLoadNativeExpressAds() only when the needed native ads and items are completely added to the mRecyclerViewItems list.

In the sample app, the filling in of data into the mRecyclerViewItems is executed synchronously and completed before calling setUpAndLoadNativeExpressAds(), which is why the exception was not encountered.

That said, your issue is more on implementation rather than anything specific to Mobile Ads SDK. I suggest that you raise your Android implementation concerns to other developer forums such as StackOverflow.

Regards,
Ivan Bautista
Mobile Ads SDK Team
Reply all
Reply to author
Forward
0 new messages