How can I make connection between retrofit and RxJava in different classes?

I have some classes as presenter and in these classes I use retrofit for some methods. But some methods are duplicated. So I want to use a class for all retrofit and connect to server methods and call them when I want. But when I created that class it has NullpointerException Error

I will be very thankful if you help me

this is presenter codes

publicclassDefinitionPresenterimplementsDefinitionContract.Presenter{privateKalaBeanDataSource kalaBeanDataSource;privateDefinitionContract.View view;privateCompositeDisposable compositeDisposable =newCompositeDisposable();privateDatabaseMethods databaseMethods;privateActivityKindList activityKindList;publicDefinitionPresenter(KalaBeanDataSource kalaBeanDataSource){this.kalaBeanDataSource = kalaBeanDataSource;  
 databaseMethods =newDatabaseMethods(kalaBeanDataSource,compositeDisposable);   
 activityKindList =newActivityKindList();}@Overridepublicvoid attachView(DefinitionContract.View view){this.view = view;}@Overridepublicvoid detachView(){  
 view =null;if(compositeDisposable !=null&& compositeDisposable.size()\>0){   
 compositeDisposable.clear();}}@Overridepublicvoid activityKind(){   
 activityKindList = databaseMethods.getActivityKind();if(activityKindList.getItems().size()\>0){   
 view.getActivityKind(activityKindList);}else{  
 view.showMessage(databaseMethods.message);}}}

And this is a class that I created for get data from server with retrofit and RxJava

publicclassDatabaseMethods{privateKalaBeanDataSource kalaBeanDataSource;privateCompositeDisposable compositeDisposable;privateActivityKindList activityKindListResult;publicString message =null;publicDatabaseMethods(KalaBeanDataSource kalaBeanDataSource,CompositeDisposable compositeDisposable){this.kalaBeanDataSource = kalaBeanDataSource;this.compositeDisposable = compositeDisposable;   
 activityKindListResult =newActivityKindList();}publicActivityKindList getActivityKind(){  
 kalaBeanDataSource.getActivityKind().subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(newSingleObserver\<ActivityKindList\>(){@Overridepublicvoid onSubscribe(Disposable d){ compositeDisposable.add(d);}@Overridepublicvoid onSuccess(ActivityKindList activityKindList){  
 activityKindListResult = activityKindList;}@Overridepublicvoid onError(Throwable e){  
 message = e.toString();}});if(message ==null&& activityKindListResult.getItems().size()\>0){return activityKindListResult;}else{returnnull;}}
1 Like

You’ll need to show us exactly where you get the error, ideally with the closest entries in the stack trace.

1 Like