简体   繁体   中英

How to fix exception when first buliding Room database in kotlin

I keep on getting this exception when first creating room database

  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getName()' on a null object reference

This is my room database code:

@Database(entities = [Channel::class], version = 1)
abstract class ChannelRoomDatabase : RoomDatabase() {

    abstract fun channelDao(): ChannelDao

    companion object {
        private var INSTANCE: ChannelRoomDatabase? = null

        fun getInstance(application: Application): ChannelRoomDatabase? {
            if (INSTANCE == null) {

                    INSTANCE = Room.databaseBuilder(application,
                            ChannelRoomDatabase::class.java,
                            "channels")
                            .build()
            }

            return INSTANCE
        }
    }

}

And the gradle code:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

    def room_version = "1.1.1"

    implementation "android.arch.persistence.room:runtime:$room_version"
    kapt "android.arch.persistence.room:compiler:$room_version"

Also I get this error when buliding the project:

Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.

Folder /home/Desktop/MySendBird/app/build/generated/source/kaptKotlin/debug


Folder /home/Desktop/MySendBird/app/build/generated/source/kaptKotlin/release


3rd-party Gradle plug-ins may be the cause

What could be the reason? I have seen similar questions here but no any clue. Please help!

Update! In my Dao I get this error

在此处输入图片说明

So how to make @Query understand that it I give it a java String not Kotlin String

See also the stacktrace:

/home/Desktop/MySendBird/app/build/tmp/kapt3/stubs/debug/ChannelRoomDatabase.java:6: 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.
public abstract class ChannelRoomDatabase extends android.arch.persistence.room.RoomDatabase {
                ^
:app:compileDebugKotlin
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/repository/ChannelRepository.kt: (16, 13): Variable 'db' is never used
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/repository/ChannelRepository.kt: (47, 23): Parameter 'channel' is never used
w: /home/Desktop/MySendBird/app/src/main/java/com/example/anna/mysendbird/view/MainActivity.kt: (179, 29): Parameter 'userId' is never used

See the similar questions:

using Room with kotlin, get exception when build the database

Null object reference when I try to build room

You are getting Android Room schema directory error

/home/Desktop/MySendBird/app/build/tmp/kapt3/stubs/debug/ChannelRoomDatabase.java:6: 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.
public abstract class ChannelRoomDatabase extends android.arch.persistence.room.RoomDatabase {

Schema export directory is not provided to the annotation processor

You need to add schema directory to your app's gradle.build as below,

android {

    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }

}

This will tell your Room annotation processor to generate Database schema and put it under directory mentioned under "room.schemaLocation"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM