Skip to content
Advertisement

Null pointer Exception in Android when using Glide

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

}

Advertisement

Answer

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
            }
        }
        
        // Add this line to show the BottomSheet
        bottomSheetDialog.show()
    }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement