如何解决Dagger Android库问题。对注入的字段具有空对象引用

时间:2019-04-11 20:28:30

标签: android dagger-2 dagger

我正在尝试首次使用dagger2 android库,并且尝试了Internet上的大多数链接都无济于事。我正在为viewmodel类中的注入字段获取空对象引用,如下所示。

public class RocketListViewModel extends AndroidViewModel {

@Inject
ApiHelperInterface apiHelperInterface;

@Inject
SchedulerProvider schedulerProvider;

private final LiveData<List<Rocket>> rocketListObservable;

private RocketResponse rocketResponse;

private CompositeDisposable mCompositeDisposable;

@Inject
public RocketListViewModel(@NonNull Application application) {
    super(application);

    mCompositeDisposable = new CompositeDisposable();
    rocketResponse = new RocketResponse();
    rocketListObservable = fetchRocketList(apiHelperInterface, schedulerProvider);
}

private LiveData<List<Rocket>> fetchRocketList(ApiHelperInterface apiHelperInterface, SchedulerProvider schedulerProvider) {

    final MutableLiveData<List<Rocket>> data = new MutableLiveData<>();

    mCompositeDisposable.add(apiHelperInterface
            .getRocketApiCall()
            .subscribeOn(schedulerProvider.io())
            .observeOn(schedulerProvider.ui())
            .subscribe(rocketResponse -> {
                        if(rocketResponse != null && rocketResponse.length() > 0){

                            Log.d("LiveData", rocketResponse.toString());
                            this.rocketResponse.setRocketList((List<Rocket>) rocketResponse);
                            data.setValue(this.rocketResponse.getRocketList());
                        }
                    }, throwable -> {
                        // TODO:
                    }
            ));

    return data;
}

public LiveData<List<Rocket>> getRocketListObservable() {
    return rocketListObservable;
    }
}

我的ApiHelper类如下所示

 @Singleton
 public class ApiHelper implements ApiHelperInterface {

    @Inject
    public ApiHelper(){

    }

   @Override
   public Single<JSONArray> getRocketApiCall() {
        return Rx2AndroidNetworking.get(ApiEndPoint.ENDPOINT_Rocket)
            .build()
            .getJSONArraySingle();
    }
}

下面是我的Appmodule和AppComponent类

@Module(subcomponents = ViewModelSubComponent.class)
 public class AppModule {

@Singleton
@Provides
Context provideContext(Application application) {
    return application;
}

@PreferenceInfo
@Provides
String providePreferenceName() {
    return AppConstants.PREF_NAME;
}

@Provides
SchedulerProvider provideSchedulerProvider() {
    return new AppSchedulerProvider();
}

@Singleton
@Provides
ApiHelperInterface provideApiHelperInterface(ApiHelper apiHelper) {
    return apiHelper;
}

@Singleton
@Provides
ViewModelProvider.Factory provideViewModelFactory(
        ViewModelSubComponent.Builder viewModelSubComponent) {

    return new RocketViewModelFactory(viewModelSubComponent.build());
}
}

AppComponent类

@Singleton
@Component(modules = {AndroidInjectionModule.class, 
AppModule.class, MainActivityModule.class})
public interface AppComponent {
 @Component.Builder
interface Builder {

    @BindsInstance
    Builder application(Application application);

    AppComponent build();
}

void inject(SpaceRocketX spaceRocketX);
}

我的android studio错误日志如下所示

原因:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“ io.reactivex.Single com.example.spacerocketx.remote.ApiHelperInterface.getRocketApiCall()”         在com.example.spacerocketx.view.ui.rocketList.RocketListViewModel.fetchRocketList(RocketListViewModel.java:55)         在com.example.spacerocketx.view.ui.rocketList.RocketListViewModel。(RocketListViewModel.java:47)

如果有人可以提供帮助,我将非常感激。

我试图在互联网上查看关于stackoverflow的其他类似问题,但无法通过此问题。

1 个答案:

答案 0 :(得分:0)

RocketListViewModel中,您混合了基于构造函数的注入和字段注入(并且您没有注入字段,这就是错误的原因)。

请尝试使ApiHelperInterface apiHelperInterfaceSchedulerProvider schedulerProvider字段成为最终字段,当然要删除它们的@Inject属性,并将它们注入RocketListViewModel构造函数中。您可能还需要在模块中提供它们。