title: Dagger2 - null instead of injected object
body: <p>For making things simple, suppose I want to inject EmailValidator from apache validators into my activity:</p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
  
      @Inject
      EmailValidator emailValidator;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  </code></pre>
  
  <p>I have a MainModule class:</p>
  
  <pre><code>@Module
  public class MainModule {
  
      @Provides
      public EmailValidator providesEmailValidator() {
          return EmailValidator.getInstance();
      }
  }
  </code></pre>
  
  <p>and MainComponent interface:</p>
  
  <pre><code>@Singleton
  @Component(modules = MainModule.class)
  public interface MainComponent {
  
      EmailValidator getEmailValidator();
  }
  </code></pre>
  
  <p>When trying to use my validator in activity, I'm getting a nullpointer exception:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.apache.commons.validator.routines.EmailValidator.isValid(java.lang.String)' on a null object reference
  </code></pre>
  
  <p>Obviously I'm missing something. I know that dagger creates component implementation for me. Should I use it? How?</p>
  
  <p>If I do the following in my onCreate method:</p>
  
  <pre><code>        emailValidator = Dagger_MainComponent.create().getEmailValidator();
  </code></pre>
  
  <p>then everything works fine. </p>
  
  <p>But I want to be able to use @Inject annotation anywhere (probably on setter/constructor instead of a field) instead.</p>
  
  <p><strong>What am I missing?</strong></p>
  
  <p>I did something similar with dagger1 and it worked. Of course I needed to call <code>ObjecGraph.inject(this)</code> in activity. What's the dagger2 equivalent?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Ok, so I've found a solution. If anyone will ever have such a problem, there are some snippets:</p>
  
  <p>1) I've created an application class:</p>
  
  <pre><code>public class EmailSenderApplication extends Application {
  
      private MainComponent component;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          component = Dagger_MainComponent
                  .create();
  
          component.inject(this);
      }
  
      public MainComponent component() {
          return component;
      }
  }
  </code></pre>
  
  <p>2) In AndroidManifest.xml:</p>
  
  <pre><code>&lt;application
          android:name=".EmailSenderApplication"
          ...
  </code></pre>
  
  <p>3) And finally, in the activity class where I want to inject some components those two ugly as hell lines:</p>
  
  <pre><code>component = ((EmailSenderApplication) getApplication()).component();
  component.inject(this);
  </code></pre>