Mes Mes - 1 year ago 134
Android Question

Injecting dataSource class to presenter gives null

I'm trying to inject

class to
using Dagger 2, but dataSource is null.

The code is below :

public class MainPresenter implements MainMVP.Presenter {

public static final String TAG = "MAIN-PRESENTER";

private MainMVP.View mainView;

DataSource dataSource;

public MainPresenter(@NonNull MainMVP.View mainView) {
this.mainView = mainView;

Log.i(TAG, "MainPresenter init");
.dataModule(new DataModule())

public void onButtonClick() {
if (dataSource != null) {


If I remove the condition that checks for null in dataSource I'm getting a
. Anyone can help with that? Isn't the constructor the right place to build the

Answer Source

You're building your component but you don't seem to be actually using it.

DataCompontent component = DaggerDataComponent.builder()
            .dataModule(new DataModule())

and add

void inject(MainPresenter presenter);

to your DataComponent interface.

As for your question about if that is the right place to build your component: we can't really answer that. That strongly depends on your code architecture.

Here's a nice example of MVP + dagger2 architecture. Maybe try following that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download