1
Android Studio 3.2
kotlin_version 1.2.41 

I am getting a UnsupportedOperationException mutablelist addAll.

I am passing a MutableList so not sure why I would get this runtime time exeception.

When I print the names of the classes I get the following collection classes

I/System.out: class java.util.ArrayList
I/System.out: class java.util.Collections$EmptyList

This is the mapper method that return a MutableList

override fun map(cursor: Cursor): MutableList<InsectDataModel> {
        val insectDataModelList: MutableList<InsectDataModel> = mutableListOf()

        cursor.moveToFirst()
        while(cursor.moveToNext()) {
            InsectDataModel().let {
                it.friendlyName = cursor.getString(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_FRIENDLY_NAME))
                it.scientificName = cursor.getString(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_SCIENTIFIC_NAME))
                it.dangerLevel = cursor.getInt(cursor.getColumnIndexOrThrow(InsectContract.COLUMN_DANGER_LEVEL))

                insectDataModelList.add(it)
            }
        }

        cursor.close()
        return insectDataModelList
    }

Adapter class that will load the list using addAll

class InsectAdapter(private var insectList: MutableList<InsectDataModel>): RecyclerView.Adapter<InsectAdapter.CustomInsectHolder>() {
    fun loadInsects(insectList: MutableList<InsectDataModel>) {
        println(insectList.javaClass)
        println(this.insectList.javaClass)

        this.insectList.addAll(insectList) /* Unsupported Exception here */

        notifyDataSetChanged()
    }
}

Calling insectAdapter

public void loadAllInsects(final Cursor cursor) {
    insectInteractorMapper = new InsectInteractorMapperImp();
    insectAdapter.loadInsects(insectInteractorMapper.map(cursor));
}
1
  • 1
    Collections$EmptyList is immutable Commented Jun 5, 2018 at 17:55

1 Answer 1

2

You didn't show how you initialize InsectAdapter but it seems like you are doing it from Java code with new InsectAdapter(Collections.emptyList()).

This issue is that Java doesn't have mutable and immutable collections the same way that Kotlin has.

For Kotlin code your class constructor looks like this:

class InsectAdapter(private var insectList: MutableList<InsectDataModel>) 

But for Java its signature is:

InsectAdapter(@NotNull List<Integer> insectList) {..}

So nothing protects you from passing wrong collection type - immutable vs mutable - from Java code to Kotlin code. And that's exactly what's happening here.

You are (probably) passing emptyList() which, as the docs say:

Returns the empty list (immutable).

And when you try to add something to it, it throws exception.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you are right, I was calling the InsectAdapter from Java. So calling kotlin from Java I should use a List<> instead because it's used by both languages. However, I thought kotlin used Java collection classes under the hood.
You could think about Kotlin collections as enhanced Java collections - enhanced with more types (immutable, mutable) and more functions. Just remember that Java has no knowledge about these enhancements and you should be more careful at the boundary of those two langauges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.