Hot questions for Handling NullPointerException in RxJava

Top 10 Java Open Source / RxJava / Handling NullPointerException

RxJava android mvp unit test NullPointerException

Question: I am new to Unit Testing in mvp, I want to make a verrry basic test to the presenter, which is responsible for making login, I just want to assert on


Here is the PresenterCode:

public LoginPresenter(LoginViewContract loginView,
                      LoginModelContract loginModel,
                      CurrentUserLoginModelContract currentUserLoginModel,
                      CompositeDisposable subscriptions) {

    this.subscriptions = subscriptions;

    this.loginView = loginView;

    this.loginModel = loginModel;

    this.currentUserLoginModel = currentUserLoginModel;

public void loginPres(LoginRequest loginRequest) {
        .subscribe(new SingleObserver<LoginResponse>() {

            public void onSubscribe(Disposable d) {

            public void onSuccess(LoginResponse loginResponse) {
            //  do something with the response
                loginView.loginSuccessMessage(token, true);

            public void onError(Throwable e) {
                Timber.e(e, "Error while trying to login");

Android here is the test code:

public class LoginPresenterTest {

LoginViewContract view;
LoginModelContract model;
CurrentUserLoginModelContract localModel;

LoginPresenter SUT;

public void setUp() throws Exception {
    compositeDisposable = new CompositeDisposable();
    SUT = new LoginPresenter(view, model, localModel, compositeDisposable);

public void name() {
    LoginResponse singleResponse = new LoginResponse();

    TestScheduler testScheduler = new TestScheduler();

            .thenReturn(Single.just(new LoginResponse()));


It just gives me NullPointerException, I think that Knowing how to test the success will help me test everything else, I have read about the TestScheduler, I tried it but did not help, I think I am doing something wrong, thanks in advance.

Answer: The problem is your TestScheduler. You should create a helper class to provide schedulers for your observable. Something likes:

class RxProvider{
     fun provideIOScheduler()
     fun provideAndroidMainScheduler()

//Then you can call the rxprovider inside your presenter:
// inside your test cases

you should mock your LoginResponse instead of calling new everywhere.