NPE while filter list with io.reactivex.Observable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



NPE while filter list with io.reactivex.Observable



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


ArrayList


String


Observable



All works fine if:


MatrixCursor suggestionsCursor =
new MatrixCursor(new StringBaseColumns._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 Objectkey++, suggestion);



But not if


MatrixCursor suggestionsCursor =
new MatrixCursor(new StringBaseColumns._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 Objectkey++, suggestion);

);



Because I get an error



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



Where am I wrong here?





Trying to work with a sequence containing nulls. Nulls are not allowed and not accepted in a flow and filter would never receive a null item. You have to pre-filter the iterable and remove nulls before it is handed to RxJava.
– akarnokd
Aug 10 at 10:16





@Anton feel free to accept one of the answers below if you think it is appropriate
– Lino
Aug 12 at 10:35





3 Answers
3



As mentioned in the comment, null values in the iterable sequence are not supported. This check is enforced in the ObservableFromIterable.java class:


null


ObservableFromIterable.java


v = ObjectHelper.requireNonNull(it.next(), "The iterator returned a null value");



This also means that a filter like this does not help


.filter(it -> it != null)



In any case, a good rule of thumb will be to handle error exceptions


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 Objectkey++, suggestion);

, error ->
/* handle exceptions here */
);



You could use the Iterable Extensions library (IxJava) for preprocessing, which let's you work with nulls:


null


Observable.fromIterable(
Ix.from(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 Objectkey++, suggestion);

,
Throwable::printStackTrace);



Both answears are good, but I found easier solution.


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



After populate data with Strings


Strings






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard