title: How to properly use Dagger2 with the new Android Architecture Components
body: <p>I'm trying to use the new Architecture Components, but I'm also still new to dagger and I'm missing stuff.</p>
  
  <p>With the below code, I'm getting a NullPointerException, can't locate where.
  Also if there's something else I need to fix or improve, please suggest.</p>
  
  <p><strong>CODE :</strong>
  <strong>ViewModel</strong></p>
  
  <pre><code>public class PostsVM extends ViewModel {
      private LiveData&lt;StoryPost&gt; post;
      private Repository          repository;
  
      @Inject
      public PostsVM(Repository repository) {
          this.repository = repository;
      }
  
      public void init() {
          if (this.post != null) {
              return;
          }
          post = repository.getPosts();
      }
  
      public LiveData&lt;StoryPost&gt; getPost() {
          return post;
      }
  }
  </code></pre>
  
  <p><strong>Repository</strong>  </p>
  
  <pre><code>@Singleton
  public class Repository {
      private final MutableLiveData&lt;StoryPost&gt; data = new MutableLiveData&lt;&gt;();
  
      public LiveData&lt;StoryPost&gt; getPosts() {
          //
          new GetUser(post.getUid()) {
              @Override
              public void onSuccess(@NonNull User user) {
                  // this is where I setValue//
                  data.setValue(post);
          }
  
          @Override
          public void onError() {
  
          }
  
          @Override
          public void userNotFound() {
  
          }
      };
      return data;
      }
  }
  </code></pre>
  
  <p><strong>Singleton Factory</strong></p>
  
  <pre><code>@Singleton
  public class ViewModelFactory implements ViewModelProvider.Factory {
  private final Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; creators;
  
  @Inject
  public ViewModelFactory(Map&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; creators) {
      this.creators = creators;
  }
  
  @SuppressWarnings("unchecked")
  @Override
  public &lt;T extends ViewModel&gt; T create(Class&lt;T&gt; modelClass) {
      Provider&lt;? extends ViewModel&gt; creator = creators.get(modelClass);
      if (creator == null) {
          for (Map.Entry&lt;Class&lt;? extends ViewModel&gt;, Provider&lt;ViewModel&gt;&gt; entry : creators.entrySet()) {
              if (modelClass.isAssignableFrom(entry.getKey())) {
                  creator = entry.getValue();
                  break;
              }
          }
      }
      if (creator == null) {
          throw new IllegalArgumentException("unknown model class " + modelClass);
      }
      try {
          return (T) creator.get();
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>@Dao
  public interface PostDao {
      @Query("SELECT * FROM posts ORDER by time DESC")
      LiveData&lt;List&lt;StoryPost&gt;&gt; getAll();
  
      @Query("SELECT * FROM posts WHERE id = :id")
      LiveData&lt;List&lt;StoryPost&gt;&gt; getPost(String id);
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      @NonNull
      void insert(StoryPost... posts);
  
      @Delete
      void delete(StoryPost post);
  
      @Update
      void update(StoryPost post);
  }
  </code></pre>
  
  <p>Then in MainActivity: </p>
  
  <pre><code>@Inject public ViewModelFactory factory;
  //...
  //*onCreate*
  
  PostsVM model = ViewModelProviders.of(this, factory).get(PostsVM.class);
  model.init();
  final Observer&lt;StoryPost&gt; observer = post -&gt; storyAdapter.insert(post);
  model.getPost().observe(this, observer);
  </code></pre>
  
  <p><strong>Logcat</strong> :</p>
  
  <pre><code>... java.lang.NullPointerException: Attempt to invoke interface method 
  'android.arch.lifecycle.ViewModel android.arch.lifecycle.ViewModelProvider
  $Factory.create(java.lang.Class)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2479)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2539)
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:168)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:150)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5665)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.arch.lifecycle.ViewModel android.arch.lifecycle.ViewModelProvider$Factory.create(java.lang.Class)' on a null object reference
                                                                             at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128)
                                                                                at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:96)
                                                                                at com.aollay.smartpaper.MainActivity.bindDatabase(MainActivity.java:238)
                                                                                at com.aollay.smartpaper.MainActivity.populateNews(MainActivity.java:233)
                                                                                at com.aollay.smartpaper.MainActivity.config(MainActivity.java:159)
                                                                                at com.aollay.smartpaper.MainActivity.onCreate(MainActivity.java:74)
                                                                                at android.app.Activity.performCreate(Activity.java:6372)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2432)
  </code></pre>