-4

In a fragment I am trying to set up a list view. In onCreateView() :

mFindFriendsOptionsListView = (ListView) view.findViewById(R.id.find_friends_list_view);
mFindFriendsOptionsAdapter = new FindFriendsOptionsAdapter(mFindFriendOptionIconIds, mFindFriendOptionTitles, mFindFriendOptionDescription);
mFindFriendsOptionsListView.setAdapter(mFindFriendsOptionsAdapter);

Here is the xml for the list view:

<ListView
        android:id="@+id/find_friends_options_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:layout_alignParentTop="true"
        android:divider="#999999"
        android:dividerHeight="1dp"/>

Here is the list element xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/option_icon_image_view"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/option_title_text_view"
                tools:text="Contacts"
                android:textSize="18sp"
                android:layout_marginBottom="6dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/option_description_text_view"
                tools:text="Upload contacts to be your friends"
                android:textSize="14sp"/>
        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:id="@+id/arrow_image_view"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp" />

</RelativeLayout>

Here is the stacktrace:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.hb.birthpay.fragment.FindFriendFragment.onCreateView(FindFriendFragment.java:66)

I have debugged and the field mFindFriendOptionsListView is null even though I set it using findViewById(). Why is the ListView field null and how do I fix this so that I no longer get a java.lang.NullPointerException?

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

1 Answers1

3

I'm guessing it's because it has a wrong id find_friends_options_list_view, and you're getting it with this id:

mFindFriendsOptionsListView = (ListView) view.findViewById(R.id.find_friends_list_view);

Change either one to correspond to the other. Make it your habit to copy and paste these things, otherwise you're gonna have a bad time.

Vucko
  • 7,371
  • 2
  • 27
  • 45