title: Inserting data to the room database from the adapter class crashes the app
body: <p>So I've been trying to insert data to the room database from the adapter class when an onclicklistener event happens. But everytime I do that my app crashes without any error message in the logcat. The only thing that happens in the event is that the database instance gets created and as soon as that happens the app crashes.</p>
  <p>My item class :</p>
  <pre class="lang-kotlin prettyprint-override"><code>
  @Entity(tableName=&quot;item&quot;)
  data class Item(
      @PrimaryKey(autoGenerate = true)
      val id: Int = 0,
      @ColumnInfo(name = &quot;path&quot;)
      val path: String,
      @ColumnInfo(name = &quot;name&quot;)
      val itemName: String,
  )
  </code></pre>
  <p>My DAO :</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Dao
  interface ItemDao {
      @Insert(onConflict = OnConflictStrategy.IGNORE)
      //suspend is not needed in newer versions of kotlin
      fun insert(item: Item)
  
  }
  </code></pre>
  <p>My ItemRoomDatabase.kt :</p>
  <pre class="lang-kotlin prettyprint-override"><code>@Database(entities = [Item::class], version = 1, exportSchema = false)
  abstract class ItemRoomDatabase : RoomDatabase() {
      abstract fun itemDao(): ItemDao
      companion object {
          @Volatile
          private var INSTANCE: ItemRoomDatabase? = null
          fun getDatabase(context: Context): ItemRoomDatabase {
              return INSTANCE ?: synchronized(this) {
                  val instance = Room.databaseBuilder(
                      context.applicationContext,
                      ItemRoomDatabase::class.java,
                      &quot;item_database&quot;
                  )
                      .fallbackToDestructiveMigration()
                      .build()
                  INSTANCE = instance
                  return instance
              }
          }
      }
  }
  </code></pre>
  <p>My Starred application code :</p>
  <pre class="lang-kotlin prettyprint-override"><code>class StarredApplication : Application() {
      val database: ItemRoomDatabase by lazy { ItemRoomDatabase.getDatabase(this) }
  
  }
  </code></pre>
  <p>And here's my InventoryViewModel.kt :</p>
  <pre class="lang-kotlin prettyprint-override"><code>class InventoryViewModel(private val itemDao: ItemDao) : ViewModel()  {
  
      private fun insertItem(item: Item) {
          viewModelScope.launch {
              itemDao.insert(item)
          }
      }
  
  
      fun isEntryValid(path: String, name: String): Boolean {
          if (path.isBlank() || name.isBlank()) {
              return false
          }
          return true
      }
  
      private fun getNewItemEntry(path: String, name: String): Item {
          return Item(
              path = path,
              itemName = name,
          )
      }
  
      fun addNewItem(path: String, name: String) {
          val newItem = getNewItemEntry(path, name)
          insertItem(newItem)
      }
  }
  
  class InventoryViewModelFactory(private val itemDao: ItemDao) : ViewModelProvider.Factory {
      override fun &lt;T : ViewModel?&gt; create(modelClass: Class&lt;T&gt;): T {
          if (modelClass.isAssignableFrom(InventoryViewModel::class.java)) {
              @Suppress(&quot;UNCHECKED_CAST&quot;)
              return InventoryViewModel(itemDao) as T
          }
          throw IllegalArgumentException(&quot;Unknown ViewModel class&quot;)
      }
  }
  </code></pre>
  <p>So in my fragment (PdfContainerFragment.kt) I create a viewModel instance of the inventory view model and pass it to the adapter class.</p>
  <pre class="lang-kotlin prettyprint-override"><code>    private val viewModel: InventoryViewModel by activityViewModels {
          InventoryViewModelFactory(
              (activity?.application as StarredApplication).database.itemDao()
          )
      }
  
  </code></pre>
  <p>So in my adapter class i try to insert data to the room database but the app crashes as soon as i click on the event.</p>
  <p>// Inside onBindViewHolder</p>
  <pre class="lang-kotlin prettyprint-override"><code>     holder.star.setOnClickListener{
  
              with(model){
                  setPath(paths)
              }
  
              var pdfName = holder.textView.text.toString()
              Log.d(&quot;PathPdfContainerAdapter&quot;, paths)
              Log.d(&quot;pdfNamePdfAdapter&quot;, pdfName)
  
              addNewItem( paths, pdfName)
              holder.star.setImageResource(R.drawable.ic_star_fill)
          }
  
  // Outside onViewBinding
  
      private fun isEntryValid(path: String, pdfName: String): Boolean {
          return roomViewModel.isEntryValid(
              path,
              pdfName,
          )
      }
  
      private fun addNewItem( path: String, name: String) {
          if(isEntryValid(path, name)) {
              roomViewModel.addNewItem(
                  path,
                  name,
              )
          }
      }
  </code></pre>
  <p>I am new to app development so please dumb it down for me to understand.</p>