title: Null when passing data to ViewModel
body: <p>So I have a <code>ViewModel</code> that retrieve query for search API. For that, I also have <code>SearchView</code> but when typing the first letter on <code>SearchView</code> the app crashed because <code>KotlinNullPointer</code> on this line inside retrofit</p>
  
  <p><code>resultsItem?.value = resultsItemList as List&lt;ResultsItem&gt;?</code></p>
  
  <p>I think I have done everything right, I tried </p>
  
  <ol>
  <li>Creating own method to pass data to ViewModel</li>
  <li>Using intent to pass data to ViewModel</li>
  <li>Defining default value inside ViewModel which works, but can't change after defined</li>
  </ol>
  
  <p><strong>Here is the code for the Fragment</strong></p>
  
  <pre><code>class Search : Fragment() {
  
  var searchAdapter: SearchAdapter? = null
  lateinit var recyclerView: RecyclerView
  lateinit var model: picodiploma.dicoding.database.picodiploma.dicoding.database.search.adapter.SearchView
  
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setHasOptionsMenu(true)
  }
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                            savedInstanceState: Bundle?): View? {
      // Inflate the layout for this fragment
      val view = inflater.inflate(R.layout.fragment_search, container, false)
  
  
      recyclerView = view.findViewById(R.id.search_result_tv)
  
  
      val layoutManager = LinearLayoutManager(context)
      recyclerView.layoutManager = layoutManager
  
      model = ViewModelProviders.of(this).get(picodiploma.dicoding.database.picodiploma.dicoding.database.search.adapter.SearchView::class.java)
  
      return view
  }
  
  override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
      inflater.inflate(R.menu.search, menu)
      val searchItem = menu.findItem(R.id.search_)
      val searchView = searchItem?.actionView as SearchView
      searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
  
          override fun onQueryTextSubmit(s: String): Boolean {
              return false
          }
  
          override fun onQueryTextChange(s: String): Boolean {
              model.query = s
              getViewData()
              return true
          }
      })
  }
  
  fun getViewData() {
  
      model.getData().observe(this, Observer { resultsItem -&gt;
  
          searchAdapter = SearchAdapter((resultsItem as ArrayList&lt;ResultsItem&gt;?)!!, this.context!!)
          recyclerView.adapter = searchAdapter
          recyclerView.visibility = View.VISIBLE
      })
  }
  }
  </code></pre>
  
  <p><strong>And the ViewModel</strong></p>
  
  <pre><code>class SearchView : ViewModel() {
  private val API_KEY = "2e08750083b7e21e96e915011d3f8e2d"
  private val TAG = SearchView::class.java.simpleName
  lateinit var query: String
  
  companion object {
      var resultsItem: MutableLiveData&lt;List&lt;ResultsItem&gt;&gt;? = null
  }
  
  fun getData(): LiveData&lt;List&lt;ResultsItem&gt;&gt; {
      if (resultsItem == null) {
          resultsItem = MutableLiveData()
          loadData()
      }
      return resultsItem as MutableLiveData&lt;List&lt;ResultsItem&gt;&gt;
  }
  
  
  private fun loadData() {
  
      val apiInterface = ApiClient.getList().create(ApiInterface::class.java)
  
      val responseCall = apiInterface.getTvSearch(API_KEY, query)
  
      responseCall.enqueue(object : Callback&lt;Response&gt; {
  
          override fun onResponse(call: Call&lt;Response&gt;, response: retrofit2.Response&lt;Response&gt;) {
              val resultsItemList = response.body()!!.results
              resultsItem?.value = resultsItemList as List&lt;ResultsItem&gt;?
          }
  
          override fun onFailure(call: Call&lt;Response&gt;, t: Throwable) {
              Log.d(TAG, t.toString())
          }
  
      })
  }
  }
  </code></pre>
  
  <p>What am I doing wrong? </p>