我有一个带有导致异常的简单空检查的代码块
kotlin.UninitializedPropertyAccessException: lateinit 属性 currentJob 尚未初始化 在 com.plcoding.posterpalfeature.ui.MainViewModel.getCurrentJob(MainViewModel.kt:40) 在 com.plcoding.posterpalfeature.ui.MainViewModel.getJobsListFromApi(MainViewModel.kt:284)
我已经试过了 -->
with(currentJob){
if (this == null) {
currentJob = jobsDataList.get(0) //maybe replace with accepted jobs depending on how details is implemented
}
else {
//update the current job using job id to get fresh object
currentJob = jobsDataList.filter {
it.job.job_id == currentJob.job.job_id
}.get(0)
}//should only ever be single object
}
这个
if (currentJob == null) {
currentJob = jobsDataList.get(0) //maybe replace with accepted jobs depending on how details is implemented
}
else {
//update the current job using job id to get fresh object
currentJob = jobsDataList.filter {
it.job.job_id == currentJob.job.job_id
}.get(0)
}
和一个 when 语句,它们都抛出相同的异常
这是我的变量
lateinit var currentJob: JobPW
有什么想法吗?这真的很奇怪。我正在考虑提出错误报告,但我不确定在哪里做。
在使用之前需要为 lateinit var 变量设置值。
要检查变量是否被初始化,你可以检查它:
this::currentJob.isInitialized