4

I am using room library and I have below mentioned entity:

@Parcelize
@Entity(tableName = "tb_option")
data class OptionsTable(
        var question_id: Int? = null,
        var option_id: Int? = null,
        var option: String? = null,
        var is_selected: Int? = null,


        @PrimaryKey(autoGenerate = true)
        var sr_no: Int = 0) : Parcelable

as you can see I have all the field declared as var but it is still showing error as:

error: Cannot find setter for field.
e: 

e:     private java.lang.Integer is_selected;
e:      



                     ^

please suggest some fix for this.

Thanks

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59

3 Answers3

8

Most of the time issue is occurring because of the following:

Problem 1:

Final field: Fields are marked with val, they are effectively final and don't have setter fields.

Solution: Replace the fields val with var. You might also need to initialize the fields.

Problem 2:

is keyword: We cannot use sqllite reserved keywords line for fields naming source e.g.

The following will cause error

 @ColumnInfo(name = "IS_ACTIVE") var isActive

Solution: The solution is:

@ColumnInfo(name = "IS_ACTIVE") var active
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
  • 1
    It seems ridiculous to me that you cannot define class properties with a prefix like 'is'. I believe it's possible to create columns with such a prefix without troubles. Anyway, thanks a lot for this information! – xarlymg89 Mar 21 '19 at 10:24
1

I removed the initialization of sr_no from

@PrimaryKey(autoGenerate = true)
        var sr_no: Int = 0

and the final code is:

@PrimaryKey(autoGenerate = true)
        var sr_no: Int

worked for me because it was an auto-generated field.

Akshay Sood
  • 6,366
  • 10
  • 36
  • 59
0

There is an issue in room orm library with kotlin java code generation.

My optional field isFavorite and compile time same error then I change my field name to favorite then compiled.

before var isFavorite: Int? = 0, working fine: var favorite: Int? = 0, Thanks

Qamar
  • 4,959
  • 1
  • 30
  • 49