1

I am trying to get the image URL from my Firestore database to create a remote image list (SlideModel) and show them in an image slider. I am getting the following error and the app crashes. I don't know what I am doing wrong.

Error

    Process: com.abc.trad, PID: 2954
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.abc.trad.ui.fragments.DashboardFragment.successAdListImageURL(DashboardFragment.kt:625)
    at com.abc.trad.firestore.FirestoreClass.getAdList$lambda-121(FirestoreClass.kt:1079)
    at com.abc.trad.firestore.FirestoreClass.lambda$1fhZA5eTJEbDTBjX4GIWQgO1m6U(Unknown Source:0)
    at com.abc.trad.firestore.-$$Lambda$FirestoreClass$1fhZA5eTJEbDTBjX4GIWQgO1m6U.onSuccess(Unknown Source:4)
    at com.google.android.gms.tasks.zzn.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8633)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

Following is the code that I have to get the data from the Firestore.

    fun getAdList(fragment: DashboardFragment) {
    mFireStore.collection("ads")
        .get()
        .addOnSuccessListener { document ->
            val adList: ArrayList<Ads> = ArrayList()
            for (i in document.documents) {

                val ad = i.toObject(Ads::class.java)
                ad!!.ad_id = i.id
                adList.add(ad)
            }
            fragment.successAdListImageURL(adList)

        }.addOnFailureListener { e ->

        }
}

Following is how I am trying to show the images in the slideshow.

    fun successAdListImageURL(adImageURL: ArrayList<Ads>){
    val remoteImages: ArrayList<SlideModel> = ArrayList()
    val numOfImage=adImageURL.size
    var i=1

    while (i<=numOfImage){
        remoteImages.add(SlideModel(adImageURL[i].image_url))
        i+=1
    }
    binding.imageSlider.setImageList(remoteImages, ScaleTypes.CENTER_INSIDE)
    binding.imageSlider.visibility=View.VISIBLE
}
Codist
  • 737
  • 8
  • 23
  • 2
    In programming count starts from 0 :) – Nika Chapidze Oct 28 '21 at 06:41
  • It solved the issue ;) . Initially, it was 0 and when it was not working for some reason, somewhere in the middle I changed this to 1. I now changed it back to 0 and removed '=' sing from the `while`. It's now working. – Codist Oct 28 '21 at 06:59

1 Answers1

2

Have you checked what is the value of numOfImage?

In fact, such a loop is not very convenient to use here. In this case, you do not need the index as such, and therefore you can use the forEach loop.

Instead your while-loop, try this:

adImageURL.forEach{
remoteImages.add(SlideModel(it.image_url)
}
kirkadev
  • 355
  • 4
  • 10