从函数返回时如何解决存储库函数内部的空指针异常?

发布于 2025-01-20 04:40:34 字数 1740 浏览 2 评论 0原文

查看模型文件代码:

    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中

所示错误行是:

  1. 在存储库中

     返回位图!  
     
  2. 在视图模型中:

      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 :

  1. In repository

      return bitmap!!  
    
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぽ尐不点ル 2025-01-27 04:40:34

只需删除!!,使用!!即表示您告诉编译器此方法将返回某物(non-null),并且如果返回null它将在运行时抛出异常

仅使用!!确定您将返回non-null数据。

在您的情况下,存储的数据是nullable,因此请改用

override suspend fun retrive(doc_name:String,uid: String)  : Bitmap? //add ? 
{
...
//remove `!!`
return bitmap
}

// also here remove "!!", and change your bitmap data type to Bitmap?
bitmap.value = repository.retrive(doc_name,uid)

如果您的接收器方法需要non-null位映射,则可以使用语句,尽管加载了nullable bitmap to image> image view 实际上是安全的。

Just remove !! , using !! means you tell the compiler that this method will return something (non-null) and if it is return null it will throw an Exception at runtime.

Only use !! when you are sure that you will return non-null data.

In your case, the data from storage are nullable , so use ? instead.

override suspend fun retrive(doc_name:String,uid: String)  : Bitmap? //add ? 
{
...
//remove `!!`
return bitmap
}

// also here remove "!!", and change your bitmap data type to Bitmap?
bitmap.value = repository.retrive(doc_name,uid)

If your receiver method required non-null bitmap, you could use if else statement, although loading a nullable bitmap to imageView is actually safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文