我在第一次创建会议室数据库时一直遇到这个异常
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getName()' on a null object reference
这是我的房间数据库代码:
@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
}
}
}
以及gradle代码:
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"
在建立项目时也会出现此错误:
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
可能是什么原因?我在这里看到了类似的问题,但没有任何线索。请帮忙!
更新!在我的刀中,我得到了这个错误
所以如何让@Query理解我给它一个Java字符串而不是Kotlin字符串
另请参阅堆栈跟踪:
/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
看到类似的问题:
using Room with kotlin, get exception when build the database
答案 0 :(得分:1)
您收到Android Room模式目录错误
/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 {
未将架构导出目录提供给注释处理器
您需要将架构目录添加到应用的gradle.build 中,如下所示,
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
}
这将告诉您的房间注释处理器生成数据库架构,并将其放在"room.schemaLocation"
下提到的目录下