title: Update a field in the firestore with a selected value of Spinner on hitting a button
body: <p>My app crashes when I hit the button to update a field in the <code>firestore</code> with spinner value.</p>
  <p>The collection name is <code>orders</code></p>
  <p>The document name is dynamic, <code>model.id</code> is used to get it.</p>
  <p>The field name is <code>order_status</code></p>
  <p>New value that needs to be updated for the field <code>order_status</code> is to be taken from the spinner.</p>
  <p>The code works fine and the field get updated when I hardcode the value, but it crashes when I want to use the selected value of <code>spinner</code></p>
  <p>Following is the error in <code>Logcat</code></p>
  <pre><code> kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
          at com.trad.ui.adapters.OrderStatusListAdapter.access$getBinding$p(OrderStatusListAdapter.kt:32)
          at com.trad.ui.adapters.OrderStatusListAdapter$onBindViewHolder$1.onClick(OrderStatusListAdapter.kt:84)
          at android.view.View.performClick(View.java:8160)
          at android.widget.TextView.performClick(TextView.java:16222)
          at android.view.View.performClickInternal(View.java:8137)
          at android.view.View.access$3700(View.java:888)
          at android.view.View$PerformClick.run(View.java:30236)
          at android.os.Handler.handleCallback(Handler.java:938)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:246)
          at android.app.ActivityThread.main(ActivityThread.java:8512)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
  </code></pre>
  <p><code>OrdersByStatusFragment.kt</code></p>
  <pre><code>class OrdersByStatusFragment : BaseFragment() {
  
      private lateinit var binding: OrderStatusLayoutBinding
  
      override fun onCreateView(
          inflater: LayoutInflater, container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
         
          return inflater.inflate(R.layout.fragment_orders_by_status, container, false)
      }
  
      override fun onResume() {
          super.onResume()
  
          getOrderStatusList()
      }
  
      private fun getOrderStatusList() {
         
          showProgressDialog(resources.getString(R.string.please_wait))
  
          FirestoreClass().getOrderStatusList(this@OrdersByStatusFragment)
      }
  
      fun successOrderStatusList(orderStatusList: ArrayList&lt;OrderStatus&gt;) {
  
          hideProgressDialog()
  
          if (orderStatusList.size &gt; 0) {
  
              rv_order_by_status.visibility = View.VISIBLE
              tv_no_orders_by_status_found.visibility = View.GONE
  
              rv_order_by_status.layoutManager = LinearLayoutManager(activity)
              rv_order_by_status.setHasFixedSize(true)
  
              val orderStatusListAdapter =
                  OrderStatusListAdapter(requireActivity(), orderStatusList)
  
              rv_order_by_status.adapter = orderStatusListAdapter
  
          } else {
  
              rv_order_by_status.visibility = View.GONE
              tv_no_orders_by_status_found.visibility = View.VISIBLE
          }
      }
  
      fun successNewOrderStatus() {
  
          Toast.makeText(requireContext(), &quot;Success&quot;, Toast.LENGTH_SHORT).show()
  
      }
  }
  </code></pre>
  <p><code>OrderStatusListAdapter.kt</code></p>
  <pre><code>open class OrderStatusListAdapter(
      private val context: Context,
      private var list: ArrayList&lt;OrderStatus&gt;,
  
      ) : RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;() {
    
      private lateinit var binding: OrderStatusLayoutBinding
  
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
  
          return MyViewHolder(OrderStatusLayoutBinding.inflate(LayoutInflater.from(parent.context), parent,false))
      }
  
      override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
          val model = list[position]
  
          if (holder is MyViewHolder) {
  
              GlideLoader(context).loadProductPicture(
                  model.image,
                  holder.itemView.iv_order_status_item_image
              )
  
              val dateFormat = &quot;dd MMM yyyy HH:mm&quot;
             
              val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
  
              val calendar: Calendar = Calendar.getInstance()
              calendar.timeInMillis = model.order_datetime
  
              val orderDateTime = formatter.format(calendar.time)
              holder.itemView.tv_order_status_order_date.text = orderDateTime
  
              holder.itemView.tv_order_status_item_name.text = model.items[0].title
              holder.itemView.tv_order_status_item_price.text = &quot;$${model.total_amount}&quot;
  
              holder.itemView.tv_order_status.text = model.order_status
              holder.itemView.tv_order_status_order_id.text = model.id
  
              holder.itemView.btn_order_status_change_status.setOnClickListener {
  
                  binding.spnOrderChangeStatus.onItemSelectedListener =
                      object : AdapterView.OnItemSelectedListener {
                          override fun onItemSelected(
                              parent: AdapterView&lt;*&gt;?,
                              view: View?,
                              position: Int,
                              id: Long
                          ) {
                              Toast.makeText(
                                  context,
                                  &quot;Selected value is ${
                                      parent?.getItemAtPosition(position).toString()
                                  }&quot;,
                                  Toast.LENGTH_SHORT
                              ).show()
  
                              val newstat = parent?.getItemAtPosition(position).toString()
                              FirestoreClass().updateOrderStatus(model.id, newstat)
  
                          }
  
                          override fun onNothingSelected(parent: AdapterView&lt;*&gt;?) {
  
                          }
                      }
  
             }
  
              holder.itemView.ib_order_status_delete_product.visibility = View.GONE
  
              holder.itemView.setOnClickListener {
                  val intent = Intent(context, SoldProductDetailsActivity::class.java)
                  intent.putExtra(Constants.EXTRA_SOLD_PRODUCT_DETAILS, model)
                  context.startActivity(intent)
              }
  
  
          }
  
      }
  
      override fun getItemCount(): Int {
          return list.size
      }
  
      inner class MyViewHolder(private val binding : OrderStatusLayoutBinding) : RecyclerView.ViewHolder(binding.root)
  }
  </code></pre>
  <p><code>FirestoreClass.kt</code></p>
  <pre><code>fun updateOrderStatus(
      orderID: String,
      newOrderStatus: String
  ) {
      mFireStore.collection(Constants.ORDERS)
          .document(orderID)
          .update(&quot;order_status&quot;, newOrderStatus)
          .addOnSuccessListener {
  
  
          }
          .addOnFailureListener {
          }
  }
  </code></pre>
  <hr />
  <p>EDIT</p>
  <hr />
  <p>Following is the new error.</p>
  <pre><code>2021-06-09 01:24:17.550 2253-2253/com.tradaxis E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.tradaxis, PID: 2253
      java.lang.NullPointerException: rv_order_by_status must not be null
          at com.tradaxis.ui.fragments.OrdersByStatusFragment.successOrderStatusList(OrdersByStatusFragment.kt:76)
          at com.tradaxis.firestore.FirestoreClass$getOrderStatusList$1.onSuccess(FirestoreClass.kt:935)
          at com.tradaxis.firestore.FirestoreClass$getOrderStatusList$1.onSuccess(FirestoreClass.kt:22)
          at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
          at android.os.Handler.handleCallback(Handler.java:938)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:246)
          at android.app.ActivityThread.main(ActivityThread.java:8512)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
  </code></pre>
  <p><code>rv_order_by_status.visibility = View.VISIBLE</code> in 'fun successOrderStatusList(orderStatusList: ArrayList)' is the code at OrdersByStatusFragment.kt:76</p>
  <p><code>fragment.successOrderStatusList(list)</code> in FirestoreClass.kt is the code at FirestoreClass.kt:935</p>
  <p><code>class FirestoreClass {</code> is the code at FirestoreClass.kt:22</p>
  <p>NOTE: There was no such error for these lines of code before making these changes.</p>