Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Contains illegal final field -Kotlin

I am using a Kotlin data class with Realm, Gson annotation for fetching Data from server.

Problem: When i run project in android studio it gives following error

Error:Class "VenderConfig" contains illegal final field "name".

I am learning Kotlin so don't have much idea about that.

My VenderConfig class is:

@RealmClass
class VenderConfig(
        @SerializedName("name")
        val name: String? = null,
        @SerializedName("website")
        val wb_url: String? = null,
        @SerializedName("icon")
        val icon: String? = null,
        @SerializedName("logo")
        val logo: String? = null,
        @SerializedName("description")
        val description: String? = null,
        @PrimaryKey
        @SerializedName("id")
        val id: Int? = null
) : RealmObject() {

}

I have also tried open keyword with the field and remove data keyword also, but it did not solve the issue.

like image 498
Ajeet Choudhary Avatar asked Jul 13 '17 06:07

Ajeet Choudhary


1 Answers

You should use var keyword to declare mutable properties. val stands for immutable (final) ones.

var name: String? = null
name = "Kotlin" // OK

val immutableName: String? = null
immutableName = "Java" // won't compile, val cannot be reassigned

For more info: Properties and Fields

Notice that I'm not familiar with Realm and this may not solve your problem.

like image 107
Alexander Romanov Avatar answered Nov 10 '22 02:11

Alexander Romanov