Search code examples
androidrealmkotlin

Error:Class "X" contains illegal final field "Y"


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

Problem: When I run project in Android Studio it gives the 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 removed data keyword, but it did not solve the issue.


Solution

  • 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