title: Kotlin coroutines: Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.postValue(java.lang.Object)' on a null object reference
body: <p>I have some errors in <em>Retrofit2</em> and <em>Kotlin Coroutines</em> technologies. I need to dynamically query an info in my service.</p>
  <p>For example, the URL is &quot;https://exampleapidomain.com/api/sub_categories/read.php?id=2&quot; I want to change <code>id</code> parameter dynamically.</p>
  <p>My service:</p>
  <pre><code>interface AltKategoriService {
  
      @GET(&quot;alt_kategoriler/&quot; + Const.READ_URL_PIECE)
      fun getId(@Query(&quot;id&quot;) id: String?): Call&lt;Resource&lt;AltKategorilerResponse&gt;&gt;
  
      companion object{
          fun build(): AltKategoriService {
              val interceptor = HttpLoggingInterceptor()
              interceptor.level = HttpLoggingInterceptor.Level.BODY
  
              val okHttpClient = OkHttpClient.Builder()
                  .addInterceptor(interceptor)
                  .build()
  
              val retrofit = Retrofit.Builder()
                  .addConverterFactory(GsonConverterFactory.create())
                  .baseUrl(Const.BASE_URL)
                  .client(okHttpClient)
                  .build()
  
              return retrofit.create(AltKategoriService::class.java)
          }
      }
  }
  </code></pre>
  <p>My <em>DataSource</em> file:</p>
  <pre><code>class RemoteAltKategorilerDataSource : AltKategorilerDataSource {
      override fun getSubCategories(): Flow&lt;Resource&lt;AltKategorilerResponse&gt;&gt; = flow {
          try {
              emit(Resource.Loading())
  
              val call = AltKategoriService.build().getId(&quot;2&quot;).execute()
              if (call.isSuccessful) {
  
                  call.body()?.let {
                      emit(it)
                  }
              }
  
          } catch (ex: Exception) {
              emit(Resource.Error(ex))
              ex.printStackTrace()
          }
      }
  }
  </code></pre>
  <p>I get the following error:</p>
  <pre><code>Attempt to invoke virtual method 'void androidx.lifecycle.MutableLiveData.postValue(java.lang.Object)' on a null object reference&quot; and then, app crashes.
  </code></pre>
  <p>I'm waiting for your answers and code examples. Thank you!</p>
  <p>Edited. My <em>ViewModel</em>:</p>
  <pre><code>class SubCategoryViewModel: ViewModel() {
      private val altKategoriRepository = AltKategoriRepository()
  
      init {
          getUsers()
      }
  
      var loading: MutableLiveData&lt;Boolean&gt;? = MutableLiveData()
      var altKategoriLiveData = MutableLiveData&lt;AltKategorilerResponse&gt;()
      var error = MutableLiveData&lt;Throwable&gt;()
  
      fun getUsers() = viewModelScope.launch {
          altKategoriRepository.getSubCategories()
              .asLiveData(viewModelScope.coroutineContext).observeForever {
  
                  when (it.status) {
                      ResourceStatus.LOADING -&gt; {
                          loading?.postValue(true)
                      }
  
                      ResourceStatus.SUCCESS -&gt; {
                          Log.e(&quot;Message&quot;, it.data.toString())
                          altKategoriLiveData.postValue(it.data!!)
                          loading?.postValue(false)
                      }
  
                      ResourceStatus.ERROR -&gt; {
                          error.postValue(it.throwable!!)
                          loading?.postValue(false)
                      }
                  }
              }
      }
  }
  </code></pre>