0

Im trying to implement a simple example with Dagger where Module returns just a string

Module is :

@Module(
    injects = { MainActivity.class }
)
 public class MyDataModule {

public MyDataModule() {
}

@Provides
public MyDataModule provideMyData() {
    return new MyDataModule();
}


public String createMyDataItems() {
    return "MyDataString";
}

}

And Activity :

@Inject MyDataModule myDataModule;

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.my_data);
    textView.setText(myDataModule.createMyDataItems());

}

}

And I get a NPE at the last line :

textView.setText(myDataModule.createMyDataItems());
Tristan
  • 3,530
  • 3
  • 30
  • 39
vicolored
  • 815
  • 2
  • 10
  • 20

1 Answers1

1

You need to instantiate the object graph with

ObjectGraph.create(new MyDataModule())

You can read more in the official tutorial.

As a side note, I recommend you try setting up Dagger 2 instead of Dagger 1

yuval
  • 6,369
  • 3
  • 32
  • 44