0

I used one of the answers from this question to get help with previous error but now I'm getting another one. There's a suggested answer to this question but I'm unable to get solution out of it for my problem.

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Object[]

private var data: Any? // fixed, can't change data type as it's in a compiled library to accept all kinds of data.

fun users() : ArrayList<User> {
    return (data as Array<*>).filterIsInstance<User>() as ArrayList<User>
}

After the suggestions in the comment, the working code looks like this but I've another side effect, I can't add items to the array, the ArrayList remains empty.

fun users() : ArrayList<User> {
    return (data as ArrayList<*>).filterIsInstance<User>() as ArrayList<User>
}

fun addItem(userVO: User) {
    users().add(user)
}

Edit 2

val users: ArrayList<User> get() = ((data as? ArrayList<Any>)?.filterIsInstance<User>() ?: emptyList()) as ArrayList<User>

fun addItem(user: User) {
    users.add(user)
}
10
  • what is data? Commented Mar 31, 2020 at 8:57
  • it is var data: Any?, adding to my question Commented Mar 31, 2020 at 8:58
  • why are you trying to cast to Array if you store ArrayList in data? Commented Mar 31, 2020 at 9:03
  • you mean it should be return (data as ArrayList<*>).filterIsInstance<User>() as ArrayList<User> Commented Mar 31, 2020 at 9:08
  • 1
    because filterIsInstance returns a new ArrayList to which you add your user Commented Mar 31, 2020 at 9:15

1 Answer 1

0

Having untyped data as a source is not the best idea but if you are working with a third-party library you might have no choice. In that case, you may try the following:

private var data: Any? = null

@Suppress("UNCHECKED_CAST")
val users: List<User> get() = (data as? ArrayList<Any>)?.filterIsInstance<User>() ?: emptyList()

fun addUser(user: User) {
   @Suppress("UNCHECKED_CAST")
   (data as? ArrayList<Any>)?.add(user)
}

In the above, I suppose that the data list may contain not only users but other entities also.

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

5 Comments

sorry, i still have the problem of having empty list because of filterIsInstance
@AppDeveloper if you use addUser function from my example then filterIsInstance can't be the problem. Can you describe your actions that lead to the "empty list because of filterIsInstance" problem?
Can you please update the code to use ArrayList in val users: List<User>, as I've remove method in use too
see my edit 2 please for the updated code. Intellij casted it for me. my add item calls the getter (unliked your example) that's why it ended up with 0 items.
Well, you can't add something into a filtered list and expect the new value in the original list. Just take a look at the filterIsInstance implementation and you will see that a new ArrayList is created there. I deliberately specify the immutable List<User> type for the users property to restrict any direct additions.

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.