Why is there an error java.lang.ExceptionInInitializerError when using an ImageSlider in a recyclerView in Kotlin?

I am trying to show images in an ImageSlider in a recyclerView. What I have done is, as the images to be fetched from Firebase storage, I created an array of SlideModel within the adapter class. There will be a minimum of 1 and a maximum of 5 images. Following is how I have done this.

My code seems to work as I can see data in the remoteImages (SlideModel ArrayList) in the Logcat. But, when I add the last line of code, which is holder.itemView.image_slider.setImageList(remoteImages, ScaleTypes.FIT), my app crashes with the following error. Without it, the app works fine but there are no images in the slider.

java.lang.ExceptionInInitializerError

There are a couple of more lines in the error but I omitted them as I think it will be enough and I can add them if you need.

I have implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0' in my build.gradle

Following is the XML code for the ImageSlider which is a part of my itemlayout for the recyclerView.

    <com.denzcoskun.imageslider.ImageSlider
    android:id="@+id/image_slider"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:iss_auto_cycle="true"
    app:iss_period="2500"
    app:iss_delay="0"/>

Following is part of my ItemListAdapter.kt.

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = list[position]

        val numOfImages = model.image_count
        val remoteImages: ArrayList<SlideModel> = ArrayList()


        if (holder is MyViewHolder) {

            when (numOfImages) {
                1 -> {
                    
                    remoteImages.add(
                        SlideModel(
                            model.image,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    Log.d("TAG", remoteImages.toString())
                    Log.d("TAG", remoteImages.size.toString())
                }
                2 -> {
                   
                    remoteImages.add(
                        SlideModel(
                            model.image,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image2,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    Log.d("TAG", remoteImages.toString())
                    Log.d("TAG", remoteImages.size.toString())
                }
                3 -> {
                    remoteImages.add(
                        SlideModel(
                            model.image,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image2,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image3,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    Log.d("TAG", remoteImages.toString())
                    Log.d("TAG", remoteImages.size.toString())
                }
                4 -> {
                    remoteImages.add(
                        SlideModel(
                            model.image,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image2,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image3,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image4,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    Log.d("TAG", remoteImages.toString())
                    Log.d("TAG", remoteImages.size.toString())
                }
                5 -> {
                    remoteImages.add(
                        SlideModel(
                            model.image,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image2,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image3,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image4,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    remoteImages.add(
                        SlideModel(
                            model.image5,
                            model.title,
                            ScaleTypes.FIT
                        )
                    )

                    Log.d("TAG", remoteImages.toString())
                    Log.d("TAG", remoteImages.size.toString())
                }
                else -> {
                    Log.d("TAG", "There are no images")
                }
            }

holder.itemView.image_slider.setImageList(remoteImages, ScaleTypes.FIT)

    }


Read more here: https://stackoverflow.com/questions/68489459/why-is-there-an-error-java-lang-exceptionininitializererror-when-using-an-images

Content Attribution

This content was originally published by Codist at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: