Skip to content

No subscriber methods are called at all, when having npe because of illegal switchIfEmpty usage #4762

@sobersanta

Description

@sobersanta

The following code consists obvious error: null is passed to switchIfEmpty (actually it's a common mistake to use switchIfEmpty instead of defaultIfEmpty).
But the problem is that the consequent NPE is not emitted to subscribers. Actually, none of the subscirber's methods is called: neither onNext, nor onError or onCompleted. This code does not produce any output to the system.out!

That of course could be fixed by simple null verification on the switchIfEmpty operator creation.
But probably its worth looking more deep into the sequence of producers in this chain, and something could be improved?
This situation caused by the described behavior, when no Observable's events are raised on the event loop, was quite difficult to find and costed us few nervous cells :)

I guess its worth investigating and fixing.

// problematic code

Observable.just(null)
        .filter(Objects::nonNull)
        .flatMap(o -> Observable.just(1))
        .switchIfEmpty(null)
        .subscribe(o -> System.out.println(o)
                , Throwable::printStackTrace
                , () -> System.out.println("completed"));

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions