question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

concatMap throw an exception if applied after a custom operator (Rx 2.x)

See original GitHub issue

Hello,

We’ve got some trouble when using concatMap when a custom operator is used in the upstream.

I think a code example worth more than thousands words. So let’s define a very simple custom operator that emits downstream the items.

import io.reactivex.Observable;
import io.reactivex.ObservableOperator;
import io.reactivex.Observer;
import io.reactivex.observers.DisposableObserver;

public class MyOperator implements ObservableOperator<Integer, Integer> {

  public static void main(String[] args) {
    Observable.range(0, 10)
        .lift(new MyOperator())
        .concatMap(Observable::just)
        .subscribe(System.out::println);
  }

  @Override
  public Observer<? super Integer> apply(Observer<? super Integer> observer) throws Exception {
    return new DisposableObserver<Integer>() {

      @Override
      public void onNext(Integer s) {
        observer.onNext(s);
      }

      @Override
      public void onError(Throwable e) {
        observer.onError(e);
      }

      @Override
      public void onComplete() {
        observer.onComplete();
      }
    };
  }
}

If you run the main function you will get a stack trace like that :

Exception in thread "main" java.lang.NullPointerException
	at io.reactivex.internal.operators.observable.ObservableConcatMap$SourceObserver.onNext(ObservableConcatMap.java:128)
	at MyOperator$1.onNext(MyOperator.java:21)
	at MyOperator$1.onNext(MyOperator.java:17)
	at io.reactivex.internal.operators.observable.ObservableRange$RangeDisposable.run(ObservableRange.java:64)
	at io.reactivex.internal.operators.observable.ObservableRange.subscribeActual(ObservableRange.java:35)
	at io.reactivex.Observable.subscribe(Observable.java:10838)
	at io.reactivex.internal.operators.observable.ObservableLift.subscribeActual(ObservableLift.java:57)
	at io.reactivex.Observable.subscribe(Observable.java:10838)
	at io.reactivex.internal.operators.observable.ObservableConcatMap.subscribeActual(ObservableConcatMap.java:52)
	at io.reactivex.Observable.subscribe(Observable.java:10838)
	at io.reactivex.Observable.subscribe(Observable.java:10824)
	at io.reactivex.Observable.subscribe(Observable.java:10727)
	at MyOperator.main(MyOperator.java:12)

Note : RxJava version is 2.1.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jcornazcommented, Aug 22, 2017

Actually my operator inherit from DisposableObserver, and I thought that was the purpose of this class, because I don’t have to (and cannot) implement “onSubscribe” anymore, and also because the example given in the “Learning RxJava book” by Thomas Nield was written the same way.

But you are right, overriding DisposableObserver.onStart() by calling observer.onSubscribe() solves the problem.

May I suggest to make the method DisposableObserver.onStart() abstract to enforce its implementation ? (as it seems to be necessary)

Thank you for your help.

0reactions
jcornazcommented, Aug 23, 2017

Ah ok. Thank you for that enlightenment. All is clear now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between concatMap and flatMap in ...
Flat map uses merge operator while concatMap uses concat operator. ... private void flatMapVsConcatMap() throws InterruptedException { Observable.just(5, 2, ...
Read more >
Observable | RxJS API Document - ReactiveX
In both cases function or method will be called when subscription to Observable is being cancelled and should be used to clean up...
Read more >
RxJs Error Handling: Complete Practical Guide
The catchError operator takes as input an Observable that might error out, and starts emitting the values of the input Observable in its...
Read more >
RxJS Operators
Operators are functions. There are two kinds of operators: Pipeable Operators are the kind that can be piped to Observables using the syntax...
Read more >
Mono (reactor-core 3.5.0)
A Reactive Streams Publisher with basic rx operators that emits at most one item via ... Create a Mono that terminates with an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found