Search code examples
androiditeratorrx-java2rx-android

NPE while filter list with io.reactivex.Observable


I facing with problem while try to filter ArrayList of String as Observable.

All works fine if:

MatrixCursor suggestionsCursor =
        new MatrixCursor(new String[]{BaseColumns._ID, COLUMN_FIELD_NO});

for (int i = 0; i < data.size(); i++) {
    if (data.get(i) != null)
        if (data.get(i).toLowerCase().contains(newText.toLowerCase()))
            filtered.add(data.get(i));
}

int key = 0;
for (String suggestion : filtered) {
    suggestionsCursor.addRow(new Object[]{key++, suggestion});
}

But not if

MatrixCursor suggestionsCursor =
        new MatrixCursor(new String[]{BaseColumns._ID, COLUMN_FIELD_NO});

Observable.fromIterable(data)
        .filter(it->it!=null && it.toLowerCase().contains(newText.toLowerCase()))
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .toList()
        .subscribe(strings -> {
            int key = 0;
            for (String suggestion : strings) {
                suggestionsCursor.addRow(new Object[]{key++, suggestion});
            }          
        });

Because I get an error

io.reactivex.exceptions.OnErrorNotImplementedException: The iterator returned a null value

Where am I wrong here?


Solution

  • Both answears are good, but I found easier solution.

    data.removeAll(Collections.singleton(null));
    

    After populate data with Strings