0

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.

Martin
  • 207
  • 2
  • 12
  • ImageView cIV = view.findViewById(R.id.gridGalleryImageView); this line returns null, from what I see in the exception. Make sure you are pointing to the right object and that it's accessible from the code piece you are running (i.e make sure it's in the xml file that you previously loaded and not in, say, fragment that was not loaded yet) – Dan Baruch Mar 20 '22 at 08:38
  • See my edit, I have added gallery_img_view.xml which I am inflating in each grid item. Now instead of setting foreground to image view I am setting foreground to its parent (Card View) and surprisingly I don't get this error and it works perfectly. Can you explain why? – Martin Mar 20 '22 at 08:46
  • And also if I select lets say first item of grid view and when I scroll it automatically selects one more item as I keep scrolling. How can I avoid this – Martin Mar 20 '22 at 08:49
  • Regarding your first comment, the code you shared seems ok, perhaps you did not call setContent earlier to this xml? maybe you did not inflate it? Regarding your second comment, I can't seem to fully understand the question and it would be better creating a new thread for that – Dan Baruch Mar 20 '22 at 08:53
  • It's unclear what `gridView` even is, or how it's adapter may bind the item views - and it's indeed quite likely, that the inflated item view doesn't have any `R.id.gridGalleryImageView`. – Martin Zeitler Mar 20 '22 at 09:05
  • The default `R.layout.list_item.xml` only has a `TextView` ...and unless the adapter would provide something else, `findViewById()` will not find something, which is simply not there. – Martin Zeitler Mar 20 '22 at 09:17

1 Answers1

-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.

Dan Baruch
  • 1,063
  • 10
  • 19
  • It is part of the gallery_img_view.xml that I am inflating into my grid view. You can check the code above. Anyway I found the solution. Thank you. – Martin Mar 20 '22 at 08:59