Hi! I'm encountering a strange behavior, wonderin...
# announcements
b
Hi! I'm encountering a strange behavior, wondering if it's a bug or expected behavior. Doing this:
Copy code
private lateinit var database: TestDatabase

fun get(context: Context): TestDatabase {
    if (!::database.isInitialized) {
        database = Room.databaseBuilder(context, TestDatabase::class.java, DATABASE_NAME).build()
    }
    return database
}
It's a trick I've seen elsewhere and it worked. But when I run it I get:
Copy code
Caused by: java.lang.NoSuchFieldError: No field database of type Lcom/example/storage/db/TestDatabase; in class Lcom/example/storage/db/TestDatabase$Companion; or its superclasses (declaration of 'com.example.storage.db.TestDatabase$Companion' appears in /data/app/com.example.test-2/base.apk)
g
Could you show some self contained sample that can reproduce your problem or give more context
e
why not use the lazy {} delegate?
private var database by lazy { Room.databaseBuilder(context, TestDatabase::class.java, DATABASE_NAME).build() }
b
@elexx I can't really do that because I don't have
context
at this moment. I can only have it later (when calling my
get
function)
k
@elexx That's for the other thread simple smile
e
Uh yea. you are totally right. missed that :X
l