-3

I'm trying to populate a listView and am getting a nullPointerException. When I debug the program, I see that the TextView's are all null, ie. the findViewById is not working for some reason. Any advice?

The favouritesActivity populates the activity_favourites listView, using favourite_list_layout as the format for each element in the list.

activity to populate lists

public class favouritesActivity extends AppCompatActivity {
    String[] titles, authors, publishers, ISBN;
    DBConnection dbConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favourites);

        ListView favouritesListView = (ListView) findViewById(R.id.favouritesListView);

        //Below populates title and author string lists
        //connecting to DB
        dbConnection = new DBConnection(this);

        //populating list with
        List<Book> favouriteBookList = dbConnection.getAllFavBooks();

        //below i am populating lists with book data (title and author)
        titles = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            titles[i] = favouriteBookList.get(i).title;
        }

        authors = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            authors[i] = favouriteBookList.get(i).authorsString;
        }

        publishers = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            publishers[i] = favouriteBookList.get(i).publisher;
        }

        ISBN = new String[favouriteBookList.size()];

        for (int i=0; i<favouriteBookList.size(); i++){
            ISBN[i] = favouriteBookList.get(i).isbn;
        }

        CustomAdapter customAdapter = new CustomAdapter();
        favouritesListView.setAdapter(customAdapter);

    }

    class CustomAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = getLayoutInflater().inflate(R.layout.favourite_list_layout, null);

            final int index = i;

            TextView titleTV = (TextView) findViewById(R.id.favouriteTitle);
            TextView authorTV = (TextView) findViewById(R.id.favouriteAuthor);
            TextView publisherTV = (TextView) findViewById(R.id.favouritePublisher);
            Button deleteFavButton = (Button) findViewById(R.id.deleteFavButton);

            titleTV.setText(titles[index]);
            authorTV.setText(authors[index]);
            publisherTV.setText(publishers[index]);
            deleteFavButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dbConnection.removeFavBook(ISBN[index]);
                }
            });
            return view;
        }
    }
}

favourite_list_layout - the layout of each element of the list

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

    <TextView
        android:id="@+id/favouriteTitle"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="16dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/favouriteAuthor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/favouriteTitle"
        android:layout_alignStart="@+id/favouriteTitle"
        android:layout_below="@+id/favouriteTitle"
        android:text="TextView" />

    <TextView
        android:id="@+id/favouritePublisher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/favouriteAuthor"
        android:layout_alignStart="@+id/favouriteAuthor"
        android:layout_below="@+id/favouriteAuthor"
        android:text="TextView" />

    <Button
        android:id="@+id/deleteFavButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/favouriteAuthor"
        android:layout_alignBottom="@+id/favouriteAuthor"
        android:layout_marginLeft="38dp"
        android:layout_marginStart="38dp"
        android:layout_toEndOf="@+id/favouriteTitle"
        android:layout_toRightOf="@+id/favouriteTitle"
        android:text="Delete" />
</RelativeLayout>
  • Just change `view = getLayoutInflater().inflate(R.layout.favourite_list_layout, null);` to `view = getLayoutInflater().inflate(R.layout.favourite_list_layout, viewGroup);` – Sergey Shustikov Nov 23 '17 at 20:36
  • Sergey, thanks for your response. I did this and then got the following error: FATAL EXCEPTION: main Process: ie.bookfast.bookfast, PID: 15657 android.view.InflateException: Binary XML file line #0: addView(View, LayoutParams) is not supported in AdapterView – Shane Monks O'Byrne Nov 23 '17 at 20:45
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Blorgbeard Nov 23 '17 at 20:45

1 Answers1

-1

You need to be calling findViewById() on the layout you inflated and not on your activity. That is, replace

TextView titleTV = (TextView) findViewById(R.id.favouriteTitle);

with

TextView titleTV = (TextView) view.findViewById(R.id.favouriteTitle);

and similarly for your other views.

laalto
  • 150,114
  • 66
  • 286
  • 303