2
votes

I started my first Android project in Kotlin. The official docs recommanded me to use Room which I did.

After adding Room I couldn't compile my project.

Cannot figure out how to save this field into database. You can consider adding a type converter for it.

Extra info about the error

warning: There are multiple good constructors and Room will pick the 
no-arg constructor. You can use the @Ignore annotation to eliminate 
unwanted constructors.
 public final class TodoData {
         ^  
 error: Cannot find setter for field.
 private final java.lang.Long id = null;
                             ^  
 error: Cannot find setter for field.
 private final java.lang.String firstName = null;
                               ^    
 error: Cannot find setter for field.
 private final java.lang.String lastName = null;
                               ^    

the version of kotlin and room are:

  • ext.kotlin_version = '1.2.31'
  • ext.room_version = '1.0.0'

My gradle.build dependencies looks like this:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

// Room
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

I Also added at the top of the file:

apply plugin: 'kotlin-kapt'

And this is my Dao:

@Entity
data class TodoData(
    @PrimaryKey(autoGenerate = true)
    var od: Long? = null,
    var firstName: String = "",
    var lastName: String = ""
 ){
       constructor(): this(od = null, firstName = "", lastName = "")
 }

What I tried:

  1. Change Kotlin version
  2. Add setters and getters (this seems not allowed in Kotlin)
  3. Set var to val

Did someone experienced the same problem and fixed it?

Thank you for your time!

ps.: I don't often post stuff on Stackoverflow Feedback on the way I present my problem would be kindly accepted.

1
Please check it once again. It will work fine with above codeSangeet Suresh
Just an addition - you should use val's instead or var's inside data class. DataClasses should be immutable. Check this for more info: kotlinlang.org/docs/reference/data-classes.html Also - remove this "constructor" thing. It's not doing anything acctuallymuminers
@muminers Thanks for the tipDeff
@SangeetSuresh I tried it again, Cleaned my project and rebuilded it -> failed. I edited my question with more detailed error logs this time.Deff
@Deff : Put "@Ignore" on data class constructorShweta Chauhan

1 Answers

0
votes

Why do you have a constructor()? You already defined the constructor with

data class TodoData(
    @PrimaryKey(autoGenerate = true)
    var od: Long? = null,
    var firstName: String = "",
    var lastName: String = ""
 )

Remove the constructor and everything should be fine.