-1

My ListView crush when its empty. I want to show on it EmptyView. I've found few 'solutions' but they didnt helped me.. I've tried ' List valueList = new ArrayList<>(); ' and with that setEmptyView working, but when ListView shouldn't be empty I only saw setEmptyView... Please help me to fix it, here is my code:

Adapter:

List<User> valueList = new ArrayList<>();

Here I've got error without ArrayList:

@Override
public int getCount()
{
    return this.valueList.size();
}

Activity:

 @Override
    protected void onPostExecute(Void result)
    {
        progressBar.setVisibility(View.GONE);
        ListView.setVisibility(View.VISIBLE);
        ListAdapterClass adapter = new ListAdapterClass(studentList, context);
        if(adapter!=null && !adapter.isEmpty()) {

            ListView.setAdapter(adapter);
        } else {

            MessagesListView.setEmptyView(findViewById(R.id.tv_no_result));
        }
     }

ERROR (without ArrayList)

java.lang.nullpointerexception: attemp to invoke interface method 'int java.util.List.size()' on a null object reference
Master
  • 37
  • 9

1 Answers1

2

Try this

@Override
public int getCount()
{
    return this.valueList != null ?  this.valueList.size() : 0;
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
GianhTran
  • 3,443
  • 2
  • 22
  • 42