title: Kotlin BitmapFactory IllegalStateException
body: <p>The app I am working on at the moment loads a fragment and renders it to the activity_main.xml , that works fine. Problem is I implemented a RecyclerView and a CardView, so as to list my skills dynamically. Getting an error that goes like:</p>
  
  <pre><code>java.lang.IllegalStateException: 
  BitmapFactory.decodeResource(ic_launcher_foreground) must not be null
  </code></pre>
  
  <p>This code is in the Kotlin fragment that loads the respective fragment (<strong>SkillFragment</strong>)</p>
  
  <pre><code>package smartline.jjba.storyofmylife
  
  import android.graphics.BitmapFactory
  import android.os.Bundle
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import android.widget.LinearLayout
  import androidx.fragment.app.Fragment
  import androidx.recyclerview.widget.RecyclerView
  import smartline.jjba.storyofmylife.R.id.rv
  import androidx.recyclerview.widget.LinearLayoutManager
  
  
  class SkillFragment : Fragment() {
  
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
          inflater.inflate(R.layout.fragment_skills, container, false)
  
  companion object {
      fun newInstance(): SkillFragment = SkillFragment()
  }
  
  fun addItemsToCard(items: ArrayList&lt;MyData&gt;) {
  
      items.run {
          add(MyData("text 1", BitmapFactory.decodeResource(resources, R.drawable.ic_launcher_foreground)))
          add(MyData("text 2", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
          add(MyData("text 3", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
          add(MyData("text 4", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
          add(MyData("text 5", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
          add(MyData("text 6", BitmapFactory.decodeResource(resources, R.drawable.ic_baseline_bar_chart_24px)))
      }
  
  }
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      val _recyclerView: RecyclerView = view.findViewById(R.id.rv)
      _recyclerView.layoutManager = LinearLayoutManager(this.context, LinearLayout.VERTICAL, false)
  
      var itemList = ArrayList&lt;MyData&gt;()
  
      addItemsToCard(itemList)
  
      //creating our adapter
      val adapter = RecyclerViewAdapter(itemList)
  
      //now adding the adapter to recyclerview
      _recyclerView.adapter = adapter
  
  
  }
  
  
  }
  </code></pre>
  
  <p>I have tried several different drawable resources, but to no avail. Also would be nice to know if there is another method I can call which would render the drawable into Bitmap</p>
  
  <p>This is my data class</p>
  
  <pre><code>package smartline.jjba.storyofmylife
  import android.graphics.Bitmap
  
  data class MyData(var text:String, var image: Bitmap)
  </code></pre>
  
  <p>This is my Adapter code</p>
  
  <pre><code>class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
      fun bindItems(data : MyData){
          val _textView:TextView = itemView.findViewById(R.id.textview)
          val _imageView:ImageView = itemView.findViewById(R.id.imageview)
          _textView.text = data.text
          _imageView.setImageBitmap(data.image)
  
          //set the onclick listener for the singlt list item
          itemView.setOnClickListener({
              Log.e("ItemClicked", data.text)
          })
      }
  
  }
  </code></pre>
  
  <p><strong>What I am looking for</strong></p>
  
  <p>Advice on how to deal with the issue, or information whether it's a known bug in Android or Kotlin. I can probably reinstall the Android Studio on the device and it will be ok again but would not want this happening, also if I can prevent it.</p>
  
  <p>Here is the exception log</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: smartline.jjba.storyofmylife, PID: 21687
  java.lang.IllegalStateException: BitmapFactory.decodeReso…e.ic_launcher_foreground) must not be null
      at smartline.jjba.storyofmylife.SkillFragment.addItemsToCard(SkillFragment.kt:28)
      at smartline.jjba.storyofmylife.SkillFragment.onViewCreated(SkillFragment.kt:44)
      at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1471)
      at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
      at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
      at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
      at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
      at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
      at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
      at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
      at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
      at android.os.Handler.handleCallback(Handler.java:873)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:6669)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  </code></pre>
  
  <p>Thanks in Advance for this great community!!!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>thanks to @Kiskae no exceptions anymore yay! 
  put a question mark in Bitmap, for Kotlin null check</p>
  
  <pre><code>data class MyData(var text:String, var image: Bitmap?)
  </code></pre>
  
  <p>but no images are loaded and the first item also isnt loaded??</p>
  
  <p><a href="https://i.stack.imgur.com/gawkM.png" rel="nofollow noreferrer">image of items being displayed</a></p>
  
  <p><a href="https://i.stack.imgur.com/n5m61.png" rel="nofollow noreferrer">image of code adding to array</a></p>