-6

I have code that ought to make my ListView clickable and bring my user to another activity. But I'm getting an error saying

Attempt to invoke virtual method 'void android.widget.ListView.setClickable(boolean)' on a null object reference

But I've no idea how it's a null object reference if the ListView is working. Here is my feed code:

public void populateList() {
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference allBooksRef = rootRef.child("All_Books");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<Book> bookModelList = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                Book book = ds.getValue(Book.class);
                bookModelList.add(book);
            }
            ListView list = (ListView) findViewById(R.id.listView);
            CustomListAdapter adapter = new CustomListAdapter(AdminFeed.this, bookModelList);
            list.setAdapter(adapter);

            list.setClickable(true);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    allBooksRef.addListenerForSingleValueEvent(valueEventListener);

    list.setClickable(true);
    list.setOnItemClickListener((AdapterView.OnItemClickListener) this);

}


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

    Intent i = new Intent(AdminFeed.this, BookIndexed.class);
    i.putExtra("ValueKey", modelList.get(position).getTitle());
    i.putExtra("ValueKey2", modelList.get(position).getAuthor());

    startActivity(i);

    title = bookModelList.get(position).getTitle();


}

Why is this throwing up such an error?

Tony Danilov
  • 430
  • 3
  • 11
  • 22
Eoghan Casey
  • 190
  • 3
  • 17
  • You did not initialize `list ` . Add `list = (ListView) findViewById(R.id.listView)` in `onCreate()`. And you do not need `list.setClickable(true);` . just use `setOnItemClickListener`. – ADM Mar 27 '18 at 13:11
  • Put this ListView list = (ListView) findViewById(R.id.listView); in the first line of populate list. The way you've done, your list view is initialised only when onDataChanged is called, but you have tried to set the list view clickable outside the function as well – Ashish Kumar Mar 27 '18 at 13:13

1 Answers1

1

Move findViewById outside ValueEventListener() like below code

Try this

public void populateList() {

    ListView list = (ListView) findViewById(R.id.listView);
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference allBooksRef = rootRef.child("All_Books");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<Book> bookModelList = new ArrayList<>();
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                Book book = ds.getValue(Book.class);
                bookModelList.add(book);
            }

            CustomListAdapter adapter = new CustomListAdapter(AdminFeed.this, bookModelList);
            list.setAdapter(adapter);

            list.setClickable(true);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    allBooksRef.addListenerForSingleValueEvent(valueEventListener);

    list.setClickable(true);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent i = new Intent(AdminFeed.this, BookIndexed.class);
            i.putExtra("ValueKey", modelList.get(position).getTitle());
            i.putExtra("ValueKey2", modelList.get(position).getAuthor());

            startActivity(i);

            title = bookModelList.get(position).getTitle();
        }
    });

}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • this hasn't worked, I now get an error when I try to navigate to the feed activity at all, nevermind when I click onto an entry in the list view. I get this error: " java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apple.BookStore/com.example.apple.BookStore.AccountActivity.AdminApplication.AdminFeed}: java.lang.ClassCastException: com.example.apple.BookStore.AccountActivity.AdminApplication.AdminFeed cannot be cast to android.widget.AdapterView$OnItemClickListener " – Eoghan Casey Mar 27 '18 at 13:21
  • @EoghanCasey try this `Intent i = new Intent(getApplicationContext(), BookIndexed.class);` – AskNilesh Mar 27 '18 at 13:22
  • @EoghanCasey checl updated ans – AskNilesh Mar 27 '18 at 13:25
  • I now have an error: " java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 " – Eoghan Casey Mar 27 '18 at 13:30