Kumar Bankesh
Kumar Bankesh

Reputation: 258

Android app crashes due to complex collection. Cannot evaluate the expression- failed to generate expression KTObject Literal exception

I am trying to cast object and it sometime crashes when object has an image url. But it works fine when I am using only string. How can I understand and resolve the issue?

val sourcedata : LinkedHashMap<String, MutableList<Any>> = somefunction()//this line is ok
var parsedData: LinkedHashMap<String, MutableList<String>> = 
  Gson().fromJson<LinkedHashMap<String, MutableList<String>>>(Gson().toJson(sourceData))!!

Also my inline function is below

inline fun <reified T> Gson.fromJson(json: String): T? = this.fromJson<T>(json, object : 
TypeToken<T>() {}.type)

Issue appears is -

Cannot evaluate the expression : Backend (JVM) Internal Error Occurred : Failed to generate expression. KTObjectLiteral expression

Upvotes: 1

Views: 237

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76619

You might want to type-check, before attempting to type-cast.

For example (just debug to see which data-types need to be considered).

if(sourcedata is LinkedHashMap<String, MutableList<String>>) {

} else if(sourcedata is LinkedHashMap<String, MutableList<Uri>>) {

} else if(sourcedata is LinkedHashMap<String, MutableList<Any>>) {

}

Without having seen that part, you might not be adding the expected type to sourcedata already. When not being able to mix data-types, you could as well use two kinds of MutableList<>.


And I really wonder what you are trying to accomplish there:

Gson().fromJson< ... >(Gson().toJson(sourceData))

Upvotes: 1

Related Questions