Dilshad
Dilshad

Reputation: 369

Unable to invoke no-args constructor for java.lang.Enum<T>

I am facing one issue with enum in Kotlin. Issue seems like silly but may be i am missing some code snippet to make it work. This is my json data

    "CustomerData": [
      {
        "token": "token_data1",
        "role": "Admin"
      },
      {
        "contactUUID": "token_data2",
        "role": "User"
      }
    ] 

One Kotlin data class as below

@JsonInclude
enum class UserRole(val userRoleDesc: String) {

@SerializedName("Admin")
ADMIN("Admin"),

@SerializedName("User")
USER("User"),

@SerializedName("unknown")
UNKNOWN("unknown");
}

and i am Using this enum class as below

@JsonInclude(JsonInclude.Include.NON_EMPTY)
data class CustomerAssociate (
        val contactUUID: String,
        val role: Enum<UserRole>?
)

But i am getting below errors

java.lang.RuntimeException: Unable to invoke no-args constructor for java.lang.Enum<T>. Registering an InstanceCreator with Gson for this type may fix this problem.

    at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)

Can some one help me out to get rid this.? Thanks in advance to all.

Upvotes: 0

Views: 477

Answers (1)

Moinkhan
Moinkhan

Reputation: 12932

Problem is in your declaraion..

@JsonInclude(JsonInclude.Include.NON_EMPTY)
data class CustomerAssociate (
        val contactUUID: String,
        val role: UserRole? // instead of your code 'val role: Enum<UserRole>?'
)

Upvotes: 1

Related Questions