Skip to content

Commit c65b3db

Browse files
ortexakarnokd
authored andcommitted
1.x: Add null check to Observable.switchIfEmpty (#4791)
* Add null check to switchIfEmpty (#4762) * Add null check to switchIfEmpty (#4762); add test
1 parent 5747017 commit c65b3db

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/main/java/rx/Observable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5403,9 +5403,14 @@ public final Observable<T> defaultIfEmpty(final T defaultValue) {
54035403
* the alternate Observable to subscribe to if the source does not emit any items
54045404
* @return an Observable that emits the items emitted by the source Observable or the items of an
54055405
* alternate Observable if the source Observable is empty.
5406+
* @throws NullPointerException
5407+
* if {@code alternate} is null
54065408
* @since 1.1.0
54075409
*/
54085410
public final Observable<T> switchIfEmpty(Observable<? extends T> alternate) {
5411+
if (alternate == null) {
5412+
throw new NullPointerException("alternate is null");
5413+
}
54095414
return lift(new OperatorSwitchIfEmpty<T>(alternate));
54105415
}
54115416

src/test/java/rx/internal/operators/OperatorSwitchIfEmptyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,9 @@ public void call() {
207207
ts.assertValueCount(2);
208208
ts.unsubscribe();
209209
}
210+
211+
@Test(expected = NullPointerException.class)
212+
public void testAlternateNull() {
213+
Observable.just(1).switchIfEmpty(null);
214+
}
210215
}

0 commit comments

Comments
 (0)