https://kotlinlang.org logo
#announcements
Title
# announcements
f

fradiati

12/09/2019, 8:23 AM
I have a List in my java file. java.util.List, and i have a function in my kotlin class which add theese items to a mutableList. I get UnsupportedOperationException and i can"t solve it with stackoverFlow posts. What can I do?
d

diesieben07

12/09/2019, 8:24 AM
How is
myMutableList
created? And can you show the stacktrace?
f

fradiati

12/09/2019, 8:26 AM
this is my adapter
Copy code
abstract class BaseRecyclerAdapter<T>(
       private var masterList: MutableList<T> = mutableListOf()
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
Copy code
fun updateList(list: List<T>) {
    masterList.clear()
    masterList.addAll(list as MutableList<T>)
    notifyDataSetChanged()
}
d

diesieben07

12/09/2019, 8:27 AM
Why are you casting to
MutableList<T>
?
addAll
accepts a plain
List<T>
, no need for a mutable list.
f

fradiati

12/09/2019, 8:27 AM
2019-12-09 091955.307 12705-12705 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.velemjaro.app.debug, PID: 12705 java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at java.util.AbstractCollection.addAll(AbstractCollection.java:344) at BaseRecyclerAdapter.updateList(BaseRecyclerViewAdapter.kt:12)
yes i know, but i try everything yes
d

diesieben07

12/09/2019, 8:28 AM
And that error should be impossible to get with that code, because
mutableListOf
returns an
ArrayList
, which overrides
add
f

fradiati

12/09/2019, 8:28 AM
yet
so it was my original function
Copy code
fun updateList(list: List<T>) {
    masterList.clear()
    masterList.addAll(list)
    notifyDataSetChanged()
}
but i get this error every time
d

diesieben07

12/09/2019, 8:29 AM
If you put a breakpoint at the start of that method, is
masterList
still an
ArrayList
?
f

fradiati

12/09/2019, 8:29 AM
Copy code
adapterBlog.updateList((List<BlogPost>) results);
masterList = {Collections$EmptryList@19456} size = 0
d

diesieben07

12/09/2019, 8:33 AM
Are you assigning
masterList
anywhere else? Like
masterList = ...
anywhere?
f

fradiati

12/09/2019, 8:34 AM
just in the constructor
d

diesieben07

12/09/2019, 8:34 AM
But then it can't be
Collections.EmptyList
...
2 Views