Search code examples
androidlistviewnullpointerexceptionnavigation-drawerlistviewitem

Getting NPE, cannot retrieve ListView content at clicked position in Navigation Drawer


I have a Navigation Drawer and want to retrieve the content inside the item in the ListView I am going to click. I can retrieve the position, but no way to link to the list view to get the content of the item clicked. in the debugger i see that is present a list view and indicate also the id left_drawer that is the same I have into the xml file and I try to retrieve:

 android.widget.ListView{634ed4d VFED.VC.. .F..H.ID 0,0-630,1731 #7f0e004f app:id/left_drawer}

so I am wondering what I am doing wrong to have a NPE and why I don't retrieve the item

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

//I have the position of the list view, and want to retrieve the content of the selected item at that position
ListView listView = (ListView) view.findViewById(R.id.left_drawer);
 String sk = (String) listView.getItemAtPosition(position);//NPE because listView is null?
        Log.d("DEBUG", "onItemClick: "+sk);

//      listView.getChildAt(0);
//listView.getSelectedItem();

}
}

And this is the error message

  Process: android.com.myapp, PID: 28051 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.ListView.getItemAtPosition(int)' on a null object reference
                                                                                at android.com.myapp.DrawerItemClickListener.onItemClick(DrawerItemClickListener.java:22)

this is Navigation Drawer

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"

    >
    <!-- The main content view -->

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_as_list"
    android:name="android.com.myApp.FragmentA"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        />

    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"

        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="19dp"
        android:background="@drawable/image_background"
        android:paddingTop="?android:attr/actionBarSize"

        android:paddingLeft="@dimen/activity_horizontal_margin"

        android:paddingRight="@dimen/activity_horizontal_margin"

        android:paddingBottom="@dimen/activity_vertical_margin"
      />
</android.support.v4.widget.DrawerLayout>

Update

In my opinion this is not a trivial NPE question. Even if I try to set a condition to eliminate the Exception was unavoidable because ListView was initiated twice once in the Main Class another time in the new class where the Google tutorial moved the Drawer object anonymous class.

Also there is any case mentioned in SO on how to retrieve an item specifically from a Navigation Drawer. Although the problem is intuitively resizable to a simple ListView.getItemAtPosition(). Putting back in one class everything is working.


Solution

  • There is logic error in your snippet, you are already inside your list's setOnItemClickListener method and you initialize your list again, then definitely it will hold nothing in it.

    yourListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    
        //get your content in Object (parent class of all )
        Object objContent = yourListView.getAdapter().getItem(position);
        String valContent = objContent .toString();
        Toast.makeText(YourMainActivity.this,"Content= "+valContent, Toast.LENGTH_LONG).show();
      }
    });