title: How to make dynamic background color change in Kotlin?
body: <p>I am trying to make the background color dynamically changing like a disco light.
  But there is an error saying that Caused by: java.lang.NullPointerException: Attempt to invoke virtual method. I added more codes as request.</p>
  <p>Here is code</p>
  <pre><code>class DiscoActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_display_blink)
  
          var btnBackground = findViewById&lt;Button&gt;(R.id.disco_btn)
          var bgColor = 1
          btnBackground.setOnClickListener {
              when (bgColor) {
                  1 -&gt; {
                      btnBackground.background = resources.getDrawable(R.drawable.btn_center_gradient)
                      bgColor = 2
                  }
                  2 -&gt; {
                      btnBackground.background = resources.getDrawable(R.drawable.btn_edge_color)
                      bgColor = 3
                  }
                  3 -&gt; {
                      btnBackground.background = resources.getDrawable(R.drawable.btn_gradient)
                      bgColor=1
                  }
              }
          }
      }
  }
  </code></pre>
  <p><em><strong>XML</strong></em></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/disco_btn&quot;
          android:layout_width=&quot;50dp&quot;
          android:layout_height=&quot;50dp&quot;
          android:background=&quot;@drawable/disco&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <p><strong>activity_display_blink</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>