0

Before I was getting Concurrent Modification Exception. I avoided it, by creating a new arraylist. But, now I'm getting a Null pointer exception & strange thing is that I've done all neccessary null checks. Still I don't understand What might went wrong.

Here is my publishResults() method:

@Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                mSuggestions.clear();
                for (Item item : mItemsAll) {
                    try {
                        if (item.name.toLowerCase(Locale.US).startsWith(constraint.toString().toLowerCase(Locale.US))) {
                            mSuggestions.add(item);
                        }
                    } catch (Exception ex) {
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = mSuggestions;
                filterResults.count = mSuggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

Here mItemsAll holds the copy of the mainList given to the adapter

@Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            @SuppressWarnings("unchecked")
            ArrayList<Item> filteredList = (ArrayList<Item>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (Item c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();

            }
        }
    };

results contains the filtered results obtained from the `performFiltering().

clear()`- clears all the elements of the main list given to the adapter &  `add(c), adds the filtered list to the list.

I was getting Concurrent Modification exceptio at this line:

for (Item c : filteredList) 

So I removed this line: ArrayList<Item> filteredList = (ArrayList<Item>) results.values; & added these lines

ArrayList<Item> filteredList = new ArrayList<Item>();
                filteredList.addAll((Collection<? extends Item>) results.values);

But now I'm getting Null Pointer exception in the getview method. But according to my code, it should go to the getview, only if the filtered resuls has some values. then Why I'm getting this exception?

Thanks in Advance..

Sangeetha Pinto
  • 21
  • 1
  • 2
  • 9
  • What is results.values? What does clear() do. What does add(Item) do? Infact, if it is possible to add the whole code structure involved, that would help. – Knossos Sep 25 '14 at 12:56
  • 1
    Can you post the `filter()` method? My guess is that you're tinkering with the original adapter items in it. If that's the case the collection you're adding items to is actually the FilterResults, which will cause that exception of course. @Knossos that's (probably) a subclass of [ArrayAdapter](http://developer.android.com/reference/android/widget/ArrayAdapter.html). – nitzanj Sep 25 '14 at 13:20
  • This has already been answerd here: http://stackoverflow.com/questions/6866238/concurrent-modification-exception-adding-to-an-arraylist – Michael Krause Sep 25 '14 at 13:31
  • Although the concept of the question is a common one, the exact reason for this question is not answered in your link. – Knossos Sep 25 '14 at 13:36

0 Answers0