数据库查询在尝试获取 LiveData 时抛出 KotlinNullPointerException

Database query throws KotlinNullPointerException while trying get LiveData

我正在尝试在 kotlin/android 中创建房间数据库,但是当我尝试从数据库中获取 LiveData 时,我得到的是 KotlinNullPointerException。我有两个表 CarRefueling,从一个片段中我可以毫无问题地获取 Car 数据,但在第二个片段中,我在尝试获取 Refueling LiveData 时出错.我找不到这两个 fragments/DAO 之间的任何区别。下面是一个简化的代码。

加油数据class:

@Entity(tableName = "refueling_table", foreignKeys = [ForeignKey(entity = Car::class,
    parentColumns = arrayOf("carID"),
    childColumns = arrayOf("carID"),
    onDelete = ForeignKey.CASCADE)]
)
data class Refueling(
    @PrimaryKey(autoGenerate = true)
    val refuelingID: Long = 0L,

    @ColumnInfo(name = "carID")
    val carID: Long = 0L,
)

RefuelingDAO:

@Dao
interface RefuelingDAO
{
    @Query("SELECT * FROM refueling_table ORDER BY refuelingID DESC")
    fun getAll(): LiveData<List<Refueling>>
}

应用数据库:

@Database(entities = [Car::class, Refueling::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase()
{
    abstract val carDAO: CarDAO
    abstract val refuelingDAO: RefuelingDAO

    companion object
    {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase
        {
            synchronized(this)
            {
                var instance = INSTANCE
                if (instance == null)
                {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        AppDatabase::class.java,
                        "app_database"
                    ).fallbackToDestructiveMigration().build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }
}

正在片段中创建 ViewModel:

val arguments = CarFragmentArgs.fromBundle(requireArguments())
val application = requireNotNull(this.activity).application
val dataSource = AppDatabase.getInstance(application).refuelingDAO
val viewModelFactory = CarFragmentVMFactory(dataSource, arguments.carID, application)

viewModel = ViewModelProvider(this, viewModelFactory).get(CarFragmentVM::class.java)

binding.carViewModel = viewModel
binding.lifecycleOwner = this
binding.buttonTest.setOnClickListener {
    viewModel.listAll()
}

ViewModel:

class CarFragmentVM(
    private val database: RefuelingDAO,
    private val carID: Long,
    application: Application
) :
        AndroidViewModel(application)
{
    val refueling = database.getAll()
   
    fun listAll()
    {
        refueling.value!!.forEach { \ IN THIS PLACE I GET ERROR
            Log.d(it)
        }
    }
}

当我点击 'buttonTest' 时,我得到了 KotlinNullPointerException。正如我所说,我在另一个片段中有相同的代码(我只是找不到任何差异)并且它工作正常但事实并非如此。任何人都可以看到我的问题在哪里或提供任何线索我应该在哪里寻找它吗?

Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fuelmanager, PID: 22828
    kotlin.KotlinNullPointerException
        at com.example.fuelmanager.ui.car.CarFragmentVM.listAll(CarFragmentVM.kt:44)
        at com.example.fuelmanager.ui.car.CarFragment$onCreateView.onClick(CarFragment.kt:44)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

第 44 行是

refueling.value!!.forEach { \ IN THIS PLACE I GET ERROR
            Log.d(it)
        }

getAll() 返回 LiveData。实际的数据库查询是在后台线程上完成的。这将需要一些时间才能完成。与此同时,valuenull。但是,您假设它会立即完成。所以,你立即尝试使用 value,然后你崩溃了 NullPointerException.

observe()LiveData