从函数返回时如何解决存储库函数内部的空指针异常?
查看模型文件代码:
fun retrive(doc_name : String,uid: String){
viewModelScope.launch(Dispatchers.IO){
bitmap.value = repository.retrive(doc_name,uid)!!
}
}
以下代码是我的存储库实现的代码
override suspend fun retrive(doc_name:String,uid: String) : Bitmap{
var bitmap :Bitmap? = null
val storageRef = FirebaseStorage.getInstance().reference?.child("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar")
val localfile = createTempFile("tempImage", null)
if (storageRef.getFile(localfile).isSuccessful) {
bitmap = BitmapFactory.decodeFile(localfile.absolutePath)
Log.d("Check","$bitmap")
}
return bitmap!!
}
,因此此功能将用于Firebase存储并检索图像并将其存储到文件中,而不是将其转换为位图。 我正在尝试从存储库中返回位图,以便我可以在UI中使用它,因为该项目正在遵循MVVM架构 但是,一旦我运行就会显示以下错误:
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.adminv1, PID: 23030
java.lang.NullPointerException
at com.example.alliaise_adminv1.Admin.Admin_Verif_rp_imp.retrive(Admin_Verif_rp_imp.kt:70)
at com.example.alliaise_adminv1.Admin.Admin_Verif_VM$retrive$1.invokeSuspend(Admin_Verif_VM.kt:30)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
因此,错误是在2个位置,第一个是在Admin存储库实现文件中,如错误行70所示,在Admin View Model Model File中相同,如错误行号30中
所示错误行是:
在存储库中
返回位图!
在视图模型中:
bitmap.value = repository.trive(doc_name,uid)!
我不确定出了什么问题,但是我有一种感觉,表明该图像正在从Firebase中检索。我已经检查了所有路径。我认为生成临时文件存在一些问题,因为这可能是唯一不确定TBH的问题。请帮忙
View model File code:
fun retrive(doc_name : String,uid: String){
viewModelScope.launch(Dispatchers.IO){
bitmap.value = repository.retrive(doc_name,uid)!!
}
}
The below code is the code of My repository Implementation
override suspend fun retrive(doc_name:String,uid: String) : Bitmap{
var bitmap :Bitmap? = null
val storageRef = FirebaseStorage.getInstance().reference?.child("/image/0XhL4jD4XCemk38rcRkIEjJMgjh2/Aadhar")
val localfile = createTempFile("tempImage", null)
if (storageRef.getFile(localfile).isSuccessful) {
bitmap = BitmapFactory.decodeFile(localfile.absolutePath)
Log.d("Check","$bitmap")
}
return bitmap!!
}
So this function is going to Firebase Storage and retrieving the image and storing it into a file and than converting it into a bitmap.
I am trying to return the bitmap from Repository so I can use it in UI as the project is following MVVM architecture
But as soon as I run it displays the following error:
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: com.example.adminv1, PID: 23030
java.lang.NullPointerException
at com.example.alliaise_adminv1.Admin.Admin_Verif_rp_imp.retrive(Admin_Verif_rp_imp.kt:70)
at com.example.alliaise_adminv1.Admin.Admin_Verif_VM$retrive$1.invokeSuspend(Admin_Verif_VM.kt:30)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
So the error is in 2 places , the first is in Admin repository implementation File as shows in error line 70 and the same in Admin View model file as shown in error Line number 30
The error lines are :
In repository
return bitmap!!
In View Model:
bitmap.value = repository.retrive(doc_name,uid)!!
I am not sure what is going wrong but I have got a feeling stating the image is falling to get Retrieved from Firebase . I have cross checked all the paths. I think there is some issue in generating a temp file as that can be the only issue not sure tbh. Kindly help please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除
!!
,使用!!
即表示您告诉编译器此方法将返回某物(non-null
),并且如果返回null
它将在运行时抛出异常
。仅使用
!!
确定您将返回non-null
数据。在您的情况下,存储的数据是
nullable
,因此请改用?
。如果您的接收器方法需要
non-null
位映射,则可以使用语句,尽管加载了nullable
bitmap toimage> image view 实际上是安全的。
Just remove
!!
, using!!
means you tell the compiler that this method will return something (non-null
) and if it is returnnull
it will throw anException
at runtime.Only use
!!
when you are sure that you will returnnon-null
data.In your case, the data from storage are
nullable
, so use?
instead.If your receiver method required
non-null
bitmap, you could useif else
statement, although loading anullable
bitmap toimageView
is actually safe.