Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 493 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot find setter for field - using Kotlin with Room database

#11
I've found that another cause of this compilation error can be due to the use of the Room's `@Ignore` annotation on fields of your entity data class:

@Entity(tableName = "foo")
data class Foo(

// Okay
@PrimaryKey
val id: String,

// Okay
val bar: String,

// Annotation causes compilation error, all fields of data class report
// the "Cannot find setter for field" error when Ignore is present
@Ignore
val causeserror: String
)

The same error also seems to happens when using the `@Transient` annotation.

I've noticed this issue using version `2.2.2` of Room:

// build.gradle file
dependencies {
...
kapt "androidx.room:room-compiler:2.2.2"
...
}

Hope that helps someone!
Reply

#12
Just use var instead of val and if you are using private keyword, make it public.

@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
var by: String,
var descendants: Int,
var score: Int,
var time: Long,
var title: String,
var type: String,
var url: String
)
Reply

#13
Since your fields are marked with `val`, they are effectively final and don't have setter fields.

Try switching out the `val` with `var`.
You might also need to initialize the fields.


@Entity(tableName = "story")
data class Story (
@PrimaryKey var id: Long? = null,
var by: String = "",
var descendants: Int = 0,
var score: Int = 0,
var time: Long = 0L,
var title: String = "",
var type: String = "",
var url: String = ""
)

**EDIT**

The above solution is a general fix for this error in Kotlin when using Kotlin with other Java libraries like Hibernate where i've seen this as well. If you want to keep immutability with Room, see some of the other answers which may be more specific to your case.

In some cases immutability with Java libraries is simply not working at all and while making sad developer noises, you have to switch that `val` for a `var` unfortunately.
Reply

#14
This error will be thrown if your column starts with Is:

@ColumnInfo(name = "IsHandicapLeague")
@NonNull
var isHandicapLeague: String = "Y"

Add a default set() function to eliminate

fun setIsHandicapLeague(flag:String) {
isHandicapLeague = flag
}



Reply

#15
If you have `@Ignore` field in the data class constructor you need to move it to class body like this:

@Entity(primaryKeys = ["id"])
data class User(
@field:SerializedName("id")
val id: Int,

@field:SerializedName("name")
val name: String,

@field:SerializedName("age")
val age: Int
) {
@Ignore
val testme: String?
}

All kudos go to [marianperca][1] on GitHub:

[To see links please register here]



[1]:

[To see links please register here]

Reply

#16
You can try to rename id variable to another name. It worked for me ;

var id: Long? = null
to

var workerId: Long? = null

If you have to name as id and you are using retrofit, then you may need to add SerializedName("id")
Reply

#17
Just make the variables mutable, change `val` into `var` for Kotlin, Or private into public for Java
Reply

#18
It seems like Room and Kotlin versions need to be matched. I have same issue with `Room 2.3.0` and `Kotlin 1.6.10` but it's ok with Kotlin `1.5.20`. It looks ok after I updated `Room` to `2.4.2`.

[To see links please register here]


Also [there is][1] a possible solution to use `@JvmOverloads constructor` for better Java compability.


[1]:

[To see links please register here]

Reply

#19
Updating Room library to the latest version [2.4.2][1] solve the issue


[1]:

[To see links please register here]

Reply

#20
I think that the variable we wrote as id is getting mixed up with the id in the system. Therefore, when I define it as uuid, my error is resolved. I think it will be solved too. Also, try using var instead of val.

@PrimaryKey(autoGenerate = true)
var uuid:Int=0
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through