Method 'boolean$CompressFormat, int,


I am having this error in my code, someone informs me how to solve it? Thank you to everyone who can help.

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean$CompressFormat, int,' on a null object reference

line error

bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)


    private fun putImgToBytearray(): ByteArray {
        val stream = ByteArrayOutputStream()
        val drawable = this.imgThumb!!.drawable as BitmapDrawable
        val bitmap = drawable.bitmap
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream)
        return stream.toByteArray()

onActivityResult code

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {
            try {
                //Getting the Bitmap from Gallery
                val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap?
                this.pictureTaken = true
            } catch (e:IOException) {
        } else {
            Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)

method for select image from gallery. it’s just an adaptation

fun openCamera() {
    try {
        val imageFile = createImageFile()
        val callCameraIntent =  Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if(callCameraIntent.resolveActivity(activity.packageManager) != null) {
            val authorities = activity.packageName + ".fileprovider"
            this.imageUri = FileProvider.getUriForFile(context, authorities, imageFile)
            callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(callCameraIntent, IMAGE_PICK_CODE)
    } catch (e: IOException) {
        Toast.makeText(context, "Could not create file!", Toast.LENGTH_SHORT).show()

Hi, it doesn’t look like a Kotlin language problem, for some reason drawable.bitmap is null.