Getting NullPointerException in getDrawable method on Glide

I have an imageview and I'll like to get the Bitmap of it...

Here is my code:

Setting the imageview:

ImageView imageView;
imageView =  findViewById(;

Getting the bitmap:

setButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        DisplayMetrics metrics = new DisplayMetrics();

        imageView.invalidate();  // I get the crash with or without that line
        Bitmap mBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();  // CRASH HERE

        // more things using the mBitmap


The crash log:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method '' on a null object reference
com.XXXX.XXXXXXX.ImageActivity$4.onClick (
android.view.View.performClick (
android.view.View$ (
android.os.Handler.handleCallback (
android.os.Handler.dispatchMessage (
android.os.Looper.loop ( (
java.lang.reflect.Method.invoke (
java.lang.reflect.Method.invoke ($ ( (      

I'm using last version of Glide:

implementation 'com.github.bumptech.glide:glide:4.9.0'                   // glide

This crash log comes from Firebase Crashlytics, a lot of users get that error, but I'm unable to reproduce it on my devices.

What's the problem? How can I fix it?


Could you please post your imageView initialization ? If not done you need to initialize it first :

ImageView imageView = findViewById(;

GifDrawable.getIntrinsicWidth Null Pointer Exception � Issue #2882 , Glide Version:compile 'com.github.bumptech.glide:glide:4.6.1' Integration libraries:None GitHub is home to over 50 million developers working together to host and review code, getDrawable()) .transition(withCrossFade()) .override( context. Method.invoke (Native Method) at The above two quote contradiction. Not necessarily, because you're in onDestroy() and after super.onDestroy() where the ImageView may have been already cleared by Glide's automatic behaviour, iff you used activity or fragment in Glide.with.

There is a method in the Glide Library in which you can get bitmap, you can use that method to get bitmap and set into Imageview.

You can also use resource drawable in place of the "imagepath".

        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                // here you will get Bitmap file and you can use it to set on image view.

            override fun onLoadCleared(placeholder: Drawable?) {
                // this is called when imageView is cleared on lifecycle call or for some other reason.
                // if you are referencing the bitmap somewhere else too other than this imageView clear it here as you can no longer have the bitmap.              

Fatal Exception: java.lang.NullPointerException Attempt to invoke , I'm using glide to load gif from drawable , it causes crashes GitHub is home to over 50 million developers working together to host and review NullPointerException Attempt to invoke virtual method 'int NullPointerException: Attempt to invoke virtual method 'int com.b.a.i.ordinal()' on a null object reference #2658 Open gemiren opened this issue Nov 30, 2017 · 4 comments

Add dependency :

implementation 'com.github.bumptech.glide:glide:3.7.0'

and write below code snippet:

                             try {
                                        .into(new SimpleTarget<Bitmap>() {
                                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                                Uri bmpUri = Common.getLocalBitmapUri(resource, context);


                                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                                super.onLoadFailed(e, errorDrawable);
                            } catch (Exception e) {

Hope this may helps you

getDrawable() gives null object while trying to get bitmap from , I am using glide for imageview and I want to get bitmap from that imageview-- NullPointerException: Attempt to invoke virtual method ' � I was calling getBitmap from onDraw() function which gets repeatedly called. Since the bitmap would only be available once the image is downloaded and loaded in imageview, any calls to get bitmap before that led to getting one of several (random) Drawable instances eg. GlideBitmapDrawable, TransitionDrawable, BitmapDrawable, etc. java code , getBitmap. method. in. Options options) { return encoder.encode(new BitmapResource(data.get(). getDrawable() == null) { fail("Drawable unexpectedly null"); } // Glide mutates Bitmaps, so it's� For the multiple getDrawable methods, it looks like ContextCompat & ResourcesCompat are part of support v4, whereas VectorDrawable backport is in support-v7. It would probably make sense to only keep AppCompatResources.getDrawable in a later version of the support lib.

android.content.res.Resources, Return a drawable object associated with a particular resource ID and styled for the specified theme. Drawable � getDrawable(int id). This method� In a RecyclerView implementation, we clear up a previously loaded GIF image when the view gets recycled. Glide Version: 4.0.0 Integration libraries: OkHttp Device/Android Version: OnePlus 3T Code: Glide.with(rootView.getContext()).clear(

Getting NullPointerException in getDrawable method on Glide, Author, Votes, Δ. K�vin Giacomino, 0, +0.00. Gaurang Goda, 0, -0.08. InsaneCat, 0, +0.01. Last visited: May 15, 2020, 6:30:33 AM. Reference variables in c# and JavaScript are similar in concept to pointers in C and C++. Reference types default to null to indicate that they are not referencing any object. Hence, if you try and access the object that is being referenced and there isn't one, you will get a NullReferenceException.

  • Check this may be it's helpful for you to get bitmap of any view.
  • Have you initalized this imageView?
  • Hey check my answer it my helps you