gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:15:53 26 4
gpt4 key购买 nike

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

加油数据类:

@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
}
}
}
}

在 fragment 中创建 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()
}

View 模型:

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。正如我所说,我在另一个 fragment 中有相同的代码(我只是找不到任何差异)并且它工作正常但事实并非如此。任何人都可以看到我的问题出在哪里或提供任何线索我应该在哪里寻找它吗?

日志:

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$1.onClick(CarFragment.kt:44)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(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

关于android - 数据库查询在尝试获取 LiveData 时抛出 KotlinNullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62593833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com