'setForeground() for image view in grid view throws Null Pointer Exception java Android Studio [duplicate]

I have a grid view in which I am inflating Image Views with images received from the device storage. Now I want to set foreground to the particular image view on which user clicks. But it throws NullPointerException. Here is my code:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView cIV = view.findViewById(R.id.gridGalleryImageView);
                cIV.setForeground(AppCompatResources.getDrawable(ImportFromGallery.this, R.drawable.checked_img_fg));
            }
        });

Here ID - gridGalleryImageView is the id for image view that I inflated into each item of the gridView.

And this is the error I am getting:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapp, PID: 22901
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setForeground(android.graphics.drawable.Drawable)' on a null object reference
        at com.example.myapp.ImportFromGallery$4.onItemClick(ImportFromGallery.java:208)
        at android.widget.AdapterView.performItemClick(AdapterView.java:330)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1221)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3216)
        at android.widget.AbsListView$3.run(AbsListView.java:4172)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8056)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

This is gallery_img_view.xml that I have inflated in each grid item

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <androidx.cardview.widget.CardView
        android:id="@+id/gridGalleryImageCardView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="centerCrop"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:cardCornerRadius="6dp"
        android:elevation="10dp"
        android:foreground="@drawable/photo_frame_bg"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/gridGalleryImageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"

            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

What could be the cause of this error. Thank you.



Solution 1:[1]

This line:

                ImageView cIV = view.findViewById(R.id.gridGalleryImageView);

You are trying to get the view (gridGalleryImageView) from "view" object. That "view" object is not your main ui but a grid (gridView) not shown in the code provided. What I guess is that the view (gridGallryImageView) is not a part of the grid you are showing and thus the find function fails to find it and returns null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Baruch