Search code examples
android-studiokotlinandroid-room

Instanciate Room database in Android Studio


I'm trying to instanciate a Room database in my main activity in Android Studio, following codelabs and tutorials, but my app always crash. Here's a part of the code:

My database (AppDatabase.kt):

@Database(entities = [Device::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun deviceDao(): DeviceDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null
        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "item_database"
                )
                    .fallbackToDestructiveMigration()
                    .build() // <---- The crash occurs here
                INSTANCE = instance
                return instance
            }
        }
    }

}

And here's the activity from which I'm trying to instantiate it:

class NavigationActivity() : AppCompatActivity() {

    private lateinit var binding: ActivityNavigationBinding
    private val db by lazy {  AppDatabase.getDatabase(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityNavigationBinding.inflate(layoutInflater)
        setContentView(binding.root)

        Log.d("instantiation", "$db") // <----- Called from here

        val navView: BottomNavigationView = binding.navView
        val navController = findNavController(R.id.nav_host_fragment_activity_navigation)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_devices, R.id.navigation_logs, R.id.navigation_settings
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }
}

Finally, here's the error message, which doesn't helps me much:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Package.getName()' on a null object reference
        at androidx.room.Room.getGeneratedImplementation(Room.java:82)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
        at AppDatabase$Companion.getDatabase(AppDatabase.kt:24)

I tried a lot of things, including ViewModel, Repository and more, but got the crash systematically, at the same point.

Here's also the part of my build.gradle file where I import Room, maybe I'm wrong in some version or whatever...

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

[...]

def roomVersion = "2.4.2"
    implementation("androidx.room:room-runtime:$roomVersion")
    kapt("androidx.room:room-compiler:$roomVersion")
    implementation "androidx.room:room-ktx:$roomVersion"

Solution

  • Make sure package declaration on top of the class is declared, for example:

        package com.macrosystems.clean.ui.core.view 
    
        import android.content.Context
        import android.content.Intent
        import android.graphics.Color
        etc...