5

When I run my app,I got follow error.

Error: Entities and Pojos must have a usable public constructor. You can have an empty
  constructor or a constructor whose parameters match the fields (by name and type).
  Tried the following constructors but they failed to match:
  ChatMsg(java.lang.String,int) : [arg0 : null, arg1 : null]
Error: Entities and Pojos must have a usable public constructor. You can have an empty
  constructor or a constructor whose parameters match the fields (by name and type).
Error: Entities and Pojos must have a usable public constructor. You can have an empty
  constructor or a constructor whose parameters match the fields (by name and type).
  Tried the following constructors but they failed to match:
  ChatMsg(java.lang.String,int) : [arg0 : null, arg1 : null]
Error: Entities and Pojos must have a usable public constructor. You can have an empty
  constructor or a constructor whose parameters match the fields (by name and type).
Warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.

That's my build.gradle for project

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()  
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.letv.sarrsdesktop:BlockCanaryExPlugin:0.9.9.2'

    }
}   
allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        maven { url "https://jitpack.io" }
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

That's my build.gradle for module

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
kapt {
    generateStubs = true
}
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "cn.sz.cyrus.kotlintest"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions{
            annotationProcessorOptions{
                includeCompileClasspath = true
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    compile 'com.github.GrenderG:Toasty:1.2.5'
    compile 'com.orhanobut:logger:1.15'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.umeng.analytics:analytics:latest.integration'
    compile 'com.github.kittinunf.fuel:fuel-android:1.7.0' //for Android
    compile 'com.github.kittinunf.fuel:fuel-rxjava:1.7.0' //for RxJava support
    compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha3"
    kapt "android.arch.persistence.room:compiler:1.0.0-alpha3"
    compile "android.arch.persistence.room:rxjava2:1.0.0-alpha3" 
}

That's my pojo class

@Entity
data class ChatMsg(var msg: String,
              var to: Int) {

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
    }

    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var createTime: Long = System.currentTimeMillis()
    var translMsg: String = ""
}

I have google my problem,but results seems can't answer my question.Who has ideas for this question . Thanks for first!

Cyrus
  • 8,995
  • 9
  • 31
  • 58
  • what are the `msg` and `to` in the constructor do you use them as columns in the table or it's supposed to be `id`, `createTime` and `translMsg`? – Oleg Osipenko Jun 27 '17 at 08:25
  • @OlegOsipenko The msg field means the main content about ChatMsg . The to field means ChatMsg sent to and its value is MASTER or ROBOT. I need to use all those fields as columns in the table. – Cyrus Jun 27 '17 at 08:30
  • so only two columns? But I see an `id` with `@PrimaryKey` annotation, so I think it's also a column – Oleg Osipenko Jun 27 '17 at 08:31
  • I remember all fields would be taken as column except I add @Ingore annotation to those fields . – Cyrus Jun 27 '17 at 08:36
  • 1
    so it's already answered: you have explanation in the stacktrace, basically either make an empty constructor with empty parameters or pass all the fields to the constructor. `You can have an empty constructor or a constructor whose parameters match the fields (by name and type)` – Oleg Osipenko Jun 27 '17 at 08:38

1 Answers1

5

The error message said exactly:

You can have an empty constructor or a constructor whose parameters match the fields (by name and type).

One thing should be noted. You should be using Annotation Site Target to put the @PrimaryKey into the correct place.

so your entity should have an empty constructor:

@Entity
data class ChatMsg(var msg: String, var to: Int) {
    constructor() : this("", UNKNOWN);

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
        private val UNKNOWN = -1; // just use for initializing
    }

    @field: PrimaryKey(autoGenerate = true)
    var id: Int = 0
}

OR should have a constructor that matches the fields.

@Entity
data class ChatMsg(
        @field:PrimaryKey(autoGenerate = true) var id: Int, 
        var createTime: Long = System.currentTimeMillis(),
        var translMsg: String = "", 
        var msg: String, var to: Int
) {

    companion object TO {
        val MASTER = 0
        val ROBOT = 1
    }

}
PiedPiper
  • 5,735
  • 1
  • 30
  • 40
holi-java
  • 29,655
  • 7
  • 72
  • 83
  • Thanks for your answer . My problem has been solved. – Cyrus Jun 27 '17 at 08:50
  • @Cyrus Not at all. – holi-java Jun 27 '17 at 08:51
  • On more question, NOTE: On the JVM, if all of the parameters of the primary constructor have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors. I copy that from [here](http://kotlinlang.org/docs/reference/classes.html#constructors).Why additional parameterless constructor can't be generated? – Cyrus Jun 27 '17 at 09:01
  • @Cyrus because it don't have any **default** parameters. – holi-java Jun 27 '17 at 09:04
  • Oh , I see. Thanks for your patience . There's always a little difficult for me to understand English documents – Cyrus Jun 27 '17 at 09:13
  • @Cyrus I'm from China. my english is so bad too. :) – holi-java Jun 27 '17 at 09:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147697/discussion-between-cyrus-and-holi-java). – Cyrus Jun 27 '17 at 09:15
  • I was getting this "POJOS" error and another one about "getters" and was using the same "data class" format as was used in multiple example projects I created (I'm following a Kotlin Jetpack Compose course) which always worked. I'm coming from Java and am new to Kotlin. I would've NEVER figured this out, but I have the Java files from the old project I'm redoing in Kotlin/Jetpack and pasted those into Android Studio which converted them to Kotlin, thankfully. – clamum Feb 11 '22 at 07:46