title: @RawQuery with ViewModel
body: <p>I followed this <a href="https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0" rel="nofollow noreferrer">Room With a View</a> tutorial and everything works as expected. Now I'm trying to implement @RawQuery and display LiveData with ViewModel. I want to display a list of items that have a certain column value (in this case a String). In <a href="https://developer.android.com/reference/android/arch/persistence/room/RawQuery" rel="nofollow noreferrer">this documentation</a> this code seems to be a solution for my desired app function: </p>
  
  <pre><code>@Dao
   interface RawDao {
       @RawQuery(observedEntities = User.class)
       LiveData&lt;List&lt;User&gt;&gt; getUsers(SupportSQLiteQuery query);
   }
   LiveData&lt;List&lt;User&gt;&gt; liveUsers = rawDao.getUsers(
       new SimpleSQLiteQuery("SELECT * FROM User ORDER BY name DESC"));
  </code></pre>
  
  <p>I want my read query to be constructed at run time when nameColumn value is passed into <code>simpleSQLiteQuery(String nameColumn)</code> function which returns <code>new SimpleSQLiteQuery</code>.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Dao</strong></p>
  
  <pre><code>@Dao
  public interface ShotDao {
  
  @RawQuery(observedEntities = Shot.class)
  LiveData&lt;List&lt;Shot&gt;&gt; getNameColumn(SupportSQLiteQuery supportSQLiteQuery);
  
  }
  </code></pre>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>    private ShotDao mShotDao;
      private LiveData&lt;List&lt;Shot&gt;&gt; mNameColumn;
      private SimpleSQLiteQuery mSimpleSQLiteQuery;
  
      public ShotRepository(Application application) {
          WrappShotDatabase database = WrappShotDatabase.getDatabase(application);
          mShotDao = database.shotDao();
          mNameColumn = mShotDao.getNameColumn(mSimpleSQLiteQuery);
      }
  
      public LiveData&lt;List&lt;Shot&gt;&gt; getNameColumn (SimpleSQLiteQuery simpleSQLiteQuery) {
          this.mSimpleSQLiteQuery = simpleSQLiteQuery;
          return mNameColumn;
      }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>    private ShotRepository mRepository;
      private LiveData&lt;List&lt;Shot&gt;&gt; mNameColumn;
      private SimpleSQLiteQuery mSimpleSQLiteQuery;
  
      public ShotViewModel(Application application) {
          super(application);
          mRepository = new ShotRepository(application);
          mNameColumn = mRepository.getNameColumn(mSimpleSQLiteQuery);
      }
  
      public LiveData&lt;List&lt;Shot&gt;&gt; getNameColumn (SimpleSQLiteQuery simpleSQLiteQuery) {
          this.mSimpleSQLiteQuery = simpleSQLiteQuery;
          return mNameColumn;
      }
  </code></pre>
  
  <p><strong>Activity</strong></p>
  
  <pre><code>        String columnName = mColumnName;
  
          shotViewModel = ViewModelProviders.of(this).get(ShotViewModel.class);
          shotViewModel.getNameColumn(simpleSQLiteQuery(columnName)).observe(this, new Observer&lt;List&lt;Shot&gt;&gt;() {
              @Override
              public void onChanged(@Nullable List&lt;Shot&gt; shots) {
                  mShotAdapter.submitList(shots);
              }
          });
  </code></pre>
  
  <p><strong>SimpleSQLiteQuery method</strong></p>
  
  <pre><code> private SimpleSQLiteQuery simpleSQLiteQuery(String nameColumn) {
          String select = "SELECT * FROM ";
          String tableName = "shots";
          String where = " WHERE nameColumn=\"";
          String close = "\"";
          return new SimpleSQLiteQuery(select + tableName + where + nameColumn + close);
      }
  </code></pre>
  
  <p>When Activity is created my app crashes and I get an error: </p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.arch.persistence.db.SupportSQLiteQuery.getSql()' on a null object reference
  </code></pre>
  
  <p>I don't quiet understand why my object is not created and when It should be. Logcat also points out row A and row B in ShotDao_Impl.java generated source file.</p>
  
  <pre><code>@Override
    public LiveData&lt;List&lt;Shot&gt;&gt; getNameColumn(SupportSQLiteQuery supportSQLiteQuery) {
      final SupportSQLiteQuery _internalQuery = supportSQLiteQuery;
      return new ComputableLiveData&lt;List&lt;Shot&gt;&gt;() { // row A
        private Observer _observer;
        @Override
        protected List&lt;Shot&gt; compute() {
          if (_observer == null) {
            _observer = new Observer("shots") {
              @Override
              public void onInvalidated(@NonNull Set&lt;String&gt; tables) {
                invalidate();
              }
            };
            __db.getInvalidationTracker().addWeakObserver(_observer);
          }
          final Cursor _cursor = __db.query(_internalQuery); // row B
          try {
            final List&lt;Shot&gt; _result = new ArrayList&lt;Shot&gt;(_cursor.getCount());
            while(_cursor.moveToNext()) {
              final Shot _item;
              _item = __entityCursorConverter_comExampleAndroidWrappDatabaseShot(_cursor);
              _result.add(_item);
            }
            return _result;
          } finally {
            _cursor.close();
          }
        }
      }.getLiveData();
    }
  </code></pre>