bcsta
bcsta

Reputation: 2307

android Null pointer exception when trying to blur a bitmap

I have an ImageView which I want to blur. I am using this method for blurring: https://futurestud.io/tutorials/how-to-blur-images-efficiently-with-androids-renderscript

This is part of the layout XML file where my ImageView is initialized:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/InfoConstraint"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:alpha="0.9"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:color/background_light" /> 

This constraint layout is also inside another constraint layout. I am then locating backdrop in a custom view with Id and extracting the bitmap out so I can then use the code in the above link to blur the bitmap. After doing so I am converting the bitmap back to the ImageView

backdrop = findViewById(R.id.backdrop);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.id.backdrop);
Bitmap blurredBitmap = BlurBuilder.blur(context, b);
backdrop.setImageBitmap(blurredBitmap);

The only issue is that I am getting a null pointer exception when I call BlurBuilder. This is the error stack :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.example.sapp.BlurBuilder.blur(BlurBuilder.java:15)
        at views.customsapp$override.init(customsapp.java:73)
        at views.customsapp$override.access$dispatch(Unknown Source:67)
        at views.customsapp.init(Unknown Source:15)
        at views.customsapp$override.init$body(customsapp.java:48)
        at views.customsapp$override.access$dispatch(Unknown Source:132)
        at views.customsapp.<init>(customsapp.java:46)
            ... 33 more

It seem like it is not finding backdrop what is the problem here? I cannot figure it out. I bet something which I am not aware of is happening when I create a bitmap out of an ImageView because the backdrop is obviously there since I am calling findViewById() which works.

update

This is the new exception that is being thrown when I try Anjana's answer

Caused by: java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1113)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1080)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1030)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:991)
E/AndroidRuntime:     at views.customsapp

.init(customsapp.java:78)
    at views.customsapp.<init>(customsapp.java:48)
        ... 28 more

Upvotes: 2

Views: 472

Answers (2)

Anjana
Anjana

Reputation: 933

Your view is not a bitmap. So it would probably return null from BitmapFactory.decodeResource() Therefore try using below code to create a bitmap

backdrop = findViewById(R.id.backdrop);
Bitmap bmp = Bitmap.createBitmap(backdrop.getWidth(), backdrop.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
backdrop.draw(c);
Bitmap blurredBitmap = BlurBuilder.blur(context, bmp);
backdrop.setImageBitmap(blurredBitmap)

Upvotes: 0

faranjit
faranjit

Reputation: 1627

It probably finds backdrop. The problem is here Bitmap b = BitmapFactory.decodeResource(getResources(), R.id.backdrop);

You pass wrong thing to decodeResource. You should pass a drawable(R.drawable.bla_bla). See this question

Upvotes: 0

Related Questions