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

#1
I'm integrating with the Room persistence library. I have a data class in Kotlin like:

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

The `@Entity` and `@PrimaryKey` annotations are for the Room library. When I try to build, it is failing with error:

Error:Cannot find setter for field.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

I also tried providing a default constructor:

@Entity(tableName = "story")
data class Story (
@PrimaryKey val id: Long,
val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) {
constructor() : this(0, "", 0, 0, 0, "", "", "")
}

But this doesn't work as well. A thing to note is that it works if I convert this Kotlin class into a Java class with getters and setters. Any help is appreciated!
Reply

#2
Another cause of this may be the naming of the field. If you use any of the pre-defined keywords, you will get the same error.
For instance, you can not name your column "is_active".

Reference:

[To see links please register here]

Reply

#3
Hey I don't know if everyone know or not, but you can not have column which is starting from `is` into `Room`.
For example you can't have like this


@Entity(tableName = "user")
data class User (
@PrimaryKey var id: Long? = null,
var userName: String = "",
var isConnectedToFB: Boolean = false,
)
Reply

#4
This is a bug and is fixed in Room `2.1.0-alpha01`

[To see links please register here]


> Bug Fixes
>
> * Room will now properly use Kotlin’s primary constructor in
> data classes avoiding the need to declare the fields as vars.
> [b/105769985][1]


[1]:

[To see links please register here]

Reply

#5
There is an issue in room db library java code generation.

I was using optional field `isFavorite`. It gives me same error then I change my field name to `favorite` then compiled.

before
` var isFavorite: Int? = 0,
`
after changing working fine
` var favorite: Int? = 0,
`
Thanks
Reply

#6
According to

[To see links please register here]

if you have an autogenerated primary key, you should write so:

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

Note that `@PrimaryKey` is written inside the class body and contains modifier `var`.

If you later want to update a row in a database with different parameters, use these lines:

val newStory = story.copy(by = "new author", title = "new title") // Cannot use "id" in object cloning
newStory.id = story.id
dao.update(newStory)

UPDATE

I still don't use AndroidX, and Room is 'android.arch.persistence.room:runtime:1.1.1'.

You can extend this class from `Serializable`. But if you want to extend it from `Parcelable`, you will get a warning (over `id` variable): `Property would not be serialized inro a 'Parcel'. Add '@IgnoredOnParcel' annotation to remove this warning`:

[![enter image description here][1]][1]

Then I moved an `id` from the body to the constructor. In Kotlin I use `@Parcelize` to create `Parcelable` classes:

@Parcelize
@Entity(tableName = "story")
data class Story (
@PrimaryKey(autoGenerate = true)
var id: Int = 0,

val by: String,
val descendants: Int,
val score: Int,
val time: Long,
val title: String,
val type: String,
val url: String
) : Parcelable

[1]:
Reply

#7
Just an update if somebody comes across this thread in 2019, after spending hours digging online on why this should work, but it doesn't.

Using `val` works as expected if you are using the AndroidX version ( `androidx.room:room-<any>:2.*`) but it doesn't when using the old `android.arch.persistence.room:<any>:1.1.1` and it seems that version `2.*` wasn't released on this latter repo.

Edit: typos
Reply

#8
If you want the `val` immutability available for your entity, it is possible.

1. You should update to AndroidX room [current version][1].
2. Check for the related [issue here][2] it is marked as **Won't Fix**
3. Now they have release a fix related to the issue with [version 2.0.0-beta01][3]
4. Now you can use immutable `val` with `default value` e.g:

```
@Entity("tbl_abc")
data class Abc(
@PrimaryKey
val id: Int = 0,
val isFavourite: Boolean = false
)
```

Previously, the above snippet will throw an error of `Cannot find setter for field`. Changing into `var` is a great workaround, but I prefer for the entity class to be immutable from outside invocation


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#9

Had this error in Java.

You cannot have a column starting with `is` or `is_` in Java.

Try renaming the column.

Another solution:

You either have to pass the field in the constructor and initialize it with the constructor argument, or create a setter for it.

Example:

public MyEntity(String name, ...) {
this.name = name;
...
}

public void setName(String name) {
this.name = name;
}


Reply

#10
You can now start your field with `is` but you can't have a number next to the `is` like : `is2FooSelected`, you have to rename to `isTwoFooSelected`.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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