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(R.id.image_wallpaper);
Glide.with(this).load(current_image.getImageURL()).into(imageView);

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 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
com.XXXX.XXXXXXX.ImageActivity$4.onClick (ImageActivity.java:170)
android.view.View.performClick (View.java:5246)
android.view.View$PerformClick.run (View.java:21200)
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:145)
android.app.ActivityThread.main (ActivityThread.java:6946)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1404)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)      

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?

Thanks

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

ImageView imageView = findViewById(R.id.myimageViewId);

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 com.android.internal.os. 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".

Glide.with(context)
        .asBitmap()
        .placeholder(placeholder)
        .error(placeholder)
        .load(
            "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.
                image.setImageBitmap(resource)
            }

            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 android.graphics. 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 {
                                Glide.with(getApplicationContext())
                                        .load(YOUR_IMAGE_URL)
                                        .asBitmap()
                                        .into(new SimpleTarget<Bitmap>() {
                                            @Override
                                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                                Uri bmpUri = Common.getLocalBitmapUri(resource, context);
                                    your_image_view.setImageBitmap(resource);

                                            }

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

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 'android.graphics.Bitmap � 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.

android.graphics.drawable.BitmapDrawable.getBitmap java code , getBitmap. method. in. android.graphics.drawable.BitmapDrawable 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.

Comments
  • Check this stackoverflow.com/a/60206330/11158194 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