1

I have done android pretty much but new to java 8. I have made a BehaviourSubject< Map< String,String>>.

I have put an observable on it. From presenter class I am setting BehaviourSbuject as observable and as soon as onClick is made the onNext is calling of the same. But it is throwing rx.exceptions.OnErrorNotImplementedException.

I am not getting why it is throwing this error as I have reffered to this http://reactivex.io/RxJava/javadoc/index.html?rx/exceptions/OnErrorNotImplementedException.html but according to this I am only made on onNext call to observable still getting this error.

MainScreenView is an interface which is implemented by MainScreenFragment.

Presenter class

public class MainScreenPresenter {
public MainScreenPresenter(MainScreenView view, MainScreenModel model) {
    Log.i("MainScreenPresenter", "default constructor");
    view.addPlace().subscribe(place -> model.place(place));
    }
}

MainScreenFragment class

public class MainScreenFragment extends Fragment implements MainScreenView, View.OnClickListener {

ArrayList<Place> places;
View mainScreenRootFragment;
Button addPlace;
TextView venueDate;
Place newPlace;
BehaviorSubject<Map<String, String>> placeAdded = BehaviorSubject.create();


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mainScreenRootFragment = inflater.inflate(R.layout.main_screen_fragment, container);
    Log.i("MainScreenFragment", "onCreateView");
    setRetainInstance(true);
    return mainScreenRootFragment;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.i("MainScreenFragment", "onViewCreated");
    initializeViews(view);
    setEventsForViews();
}

private void setEventsForViews() {
    addPlace.setOnClickListener(this);
}

private void initializeViews(View view) {
    addPlace = (Button) view.findViewById(R.id.add_place);
    venueDate = (TextView) view.findViewById(R.id.venue_date);
    places = new ArrayList<>();
}

@Override
public Observable<Map<String, String>> addPlace() {
    Log.i("MainScreenFragment", "addPlace");
    return placeAdded.asObservable();
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.add_place) {
        Log.i("MainScreenFragment", "add_place button clicked");
        placeData();
        Log.i("MainScreenFragment", "after onNext()");
    } else if (v.getId() == R.id.rv) {

    }
}

private void placeData() {
    Log.i("MainScreenFragment", "data is mapping in placeData()");
    Map<String, String> map = new HashMap<>();

    map.put("placeName", "Lonavala");
    map.put("placeDate", "20/5/2015");
    placeAdded.onNext(map);
    }
}

Errors

07-10 10:14:25.414    2754-2754/com.example.vikky.hisab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41703d58)
07-10 10:14:25.424    2754-2754/com.example.vikky.hisab E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.vikky.hisab, PID: 2754
rx.exceptions.OnErrorNotImplementedException
        at rx.Observable$31.onError(Observable.java:7204)
        at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:127)
        at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:96)
        at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:111)
        at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150)
        at rx.subjects.SubjectSubscriptionManager$SubjectObserver.emitNext(SubjectSubscriptionManager.java:254)
        at rx.subjects.BehaviorSubject.onNext(BehaviorSubject.java:166)
        at com.example.vikky.hisab.MainScreenFragment.placeData(MainScreenFragment.java:115)
        at com.example.vikky.hisab.MainScreenFragment.onClick(MainScreenFragment.java:100)
        at android.view.View.performClick(View.java:4444)
        at android.view.View$PerformClick.run(View.java:18440)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5050)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.vikky.hisab.MainScreenPresenter.lambda$new$5(MainScreenPresenter.java:11)
        at com.example.vikky.hisab.MainScreenPresenter.access$lambda$0(MainScreenPresenter.java)
        at com.example.vikky.hisab.MainScreenPresenter$$Lambda$1.call(Unknown Source)
        at rx.Observable$31.onNext(Observable.java:7209)
        at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:104)

Thanks in advance

Vikas Chandra
  • 565
  • 1
  • 9
  • 22

1 Answers1

1

It appears to me that in your OnNext handler you are throwing a NullPointerException. My guess is that then RxJava is then trying to catch this and publish the exception to the OnError handler which you have not provided. This then is escalated to a OnErrorNotImplementedException.

So two things to do here:

  1. Good practice to always have an OnError handler. If you dont there is no way to catch the exception in the async world.
  2. My guess is that model is null, so calling model.place(place) is the thing throwing NPE. Perhaps it is something in your place method, but the stack trace seems to point to model being null.
Lee Campbell
  • 10,631
  • 1
  • 34
  • 29