Home > Software design >  Null pointer Exception in Android when using Glide
Null pointer Exception in Android when using Glide

Time:06-21

Here is the logcat output

2022-06-20 13:19:27.605 20830-20830/com.example.moveapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.moveapplication, PID: 20830
java.lang.NullPointerException: Argument must not be null
    at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
    at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
    at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:768)
    at com.example.moveapplication.activities.MainActivity.openBottomSheetDialog$lambda-2(MainActivity.kt:100)
    at com.example.moveapplication.activities.MainActivity.$r8$lambda$acEx44X-0qqDdGYpms4KzfGag5A(Unknown Source:0)
    at com.example.moveapplication.activities.MainActivity$$ExternalSyntheticLambda3.onSuccess(Unknown Source:15)
    at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
    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)

From my logcat the exception occurs when using Glide to load image. Line 100 is .into(uImage)

 private fun openBottomSheetDialog(position: Int) {
    val uImage = findViewById<ImageView>(R.id.sheetIvProfileImage)
    val uNumber = findViewById<TextView>(R.id.sheetTvOwnerNumber)
    val uEmail = findViewById<TextView>(R.id.tvOwnerEmail)
    val uAbout = findViewById<TextView>(R.id.tvOwnerAbout)
    val uTitle = findViewById<TextView>(R.id.sheetTvOwnerTitle)
    val publisher = posts[position].publisher

    val ownersRef = FirebaseFirestore.getInstance().collection("owners").document(publisher)
    ownersRef.get().addOnSuccessListener { document ->
        val ownerModel = document.toObject(Owner::class.java)
        if (ownerModel != null) {
            Glide.with(this)
                .load(ownerModel.profileImage)
                .into(uImage)
        }
        uTitle.text = ownerModel?.username
        uNumber.text = ownerModel?.phoneNumber
        uEmail.text = ownerModel?.email
        uAbout.text = ownerModel?.about
    }

    val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialogTheme)
    val bottomSheetView = LayoutInflater.from(applicationContext).inflate(
            R.layout.bottom_sheet_dialog,
            findViewById(R.id.bottomSheet)
    )
    bottomSheetDialog.apply {
        setContentView(bottomSheetView)
        setCancelable(true)
    }

}

CodePudding user response:

this in Glide.with(this) it represent what ? You should to pass a view or a context there

Glide.with(requireContext())  //for example
.load(ownerModel.profileImage)
.into(uImage)

CodePudding user response:

It's hard to tell where the null is exactly because we don't have your project but you do, so a breakpoint would have clarified this for you and everyone else.

ownersRef.get().addOnSuccessListener { document ->
        val ownerModel = document.toObject(Owner::class.java)
        if (ownerModel != null) {
            Glide.with(this)
                .load(ownerModel.profileImage)
                .into(uImage)
        }

In this code, there can be various nulls.

ownerRef you do get() on firebase, but are you sure that's not null?

this in Glide.with(...) this there likely refers to the anonymous instance of addOnSuccessListener in the lambda, so not the Activity or Fragment.

ownerModel is not null because you check

uImage could be null, but I don't know if Glide cares.

Put a breakpoint, debug your app, and notice you have a NullPointerReference exception.

CodePudding user response:

As far as I understand, you're initializing the Views of BottomSheet wrong. If I'm not wrong, you're trying to show this BottomSheet inside an Activity, if yes, try the below code:

    private fun openBottomSheetDialog(position: Int) {
        // Initialize the View of the BottomSheet
        val bottomSheetView = LayoutInflater.from(this).inflate(R.layout.bottom_sheet_dialog, findViewById(R.id.bottomSheet), false)
        // Initialize the BottomSheetDialog
        val bottomSheetDialog = BottomSheetDialog(this, R.style.BottomSheetDialogTheme).apply {
            setCancelable(true)
            // Set the View
            setContentView(bottomSheetView)
        }
        /*
        * To initialize the Views of BottomSheet,
        * use parent view of the BottomSheet, i.e., bottomSheetView
        * */
        val uImage = bottomSheetView.findViewById<ImageView>(R.id.sheetIvProfileImage)
        val uNumber = bottomSheetView.findViewById<TextView>(R.id.sheetTvOwnerNumber)
        val uEmail = bottomSheetView.findViewById<TextView>(R.id.tvOwnerEmail)
        val uAbout = bottomSheetView.findViewById<TextView>(R.id.tvOwnerAbout)
        val uTitle = bottomSheetView.findViewById<TextView>(R.id.sheetTvOwnerTitle)
        val publisher = posts[position].publisher

        val ownersRef = FirebaseFirestore.getInstance().collection("owners").document(publisher)
        ownersRef.get().addOnSuccessListener { document ->
            val ownerModel = document.toObject(Owner::class.java)
            if (ownerModel != null) {
                Glide.with(this)
                    .load(ownerModel.profileImage)
                    .into(uImage)
                // These Views should be inside
                uTitle.text = ownerModel?.username
                uNumber.text = ownerModel?.phoneNumber
                uEmail.text = ownerModel?.email
                uAbout.text = ownerModel?.about
            }
        }
    }
  • Related