title: Null cast in Android RecyclerView won't go away even if I don't do any cast at all
body: <p>I'm having a very strange null cast error on my <code>RecyclerView</code>. The code that gives the error is shown below, but I also provide the entire code <a href="https://pastebin.com/D92WRekL" rel="nofollow noreferrer">here</a></p>
  
  <pre><code>private val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
          ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT or ItemTouchHelper.LEFT
      ) {
  
          var dragFrom = -1
          var dragTo = -1
          override fun onMove(
              recyclerView: RecyclerView,
              viewHolder: RecyclerView.ViewHolder,
              target: RecyclerView.ViewHolder
          ): Boolean {
  
              val fromPos = viewHolder.adapterPosition
              val toPos = target.adapterPosition
              if (dragFrom == -1) {
                  dragFrom = fromPos
              }
              dragTo = toPos
              effectList.add(toPos, effectList.removeAt(fromPos))
              recyclerView.adapter?.notifyItemMoved(fromPos, toPos)
              return true
          }
  
          override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
              Log.d(TAG, "onswiped!");
  
              val position = viewHolder.adapterPosition
              effectList.removeAt(position)
              mRecyclerView.adapter?.notifyItemRemoved(position)
              NativeInterface.removeEffectAt(position)
  
              for (i in position until effectList.size) {
                  var holder = mRecyclerView.findViewHolderForAdapterPosition(i) as? EffectsHolder
                  holder?.bindEffect(holder.effect, i)
              }
  
  
          }
  
          override fun clearView(
              recyclerView: RecyclerView,
              viewHolder: RecyclerView.ViewHolder
          ) {
              super.clearView(recyclerView, viewHolder)
              if (dragFrom != -1 &amp;&amp; dragTo != -1 &amp;&amp; dragFrom != dragTo) {
                  rotateItems(dragFrom, dragTo)
              }
              dragFrom = -1
              dragTo = -1
          }
  
          override fun isLongPressDragEnabled(): Boolean = false
          override fun isItemViewSwipeEnabled(): Boolean = false
  
          fun rotateItems(fromPos: Int, toPos: Int) {
              Log.d(TAG, "onrotate!!");
              val a = min(fromPos, toPos)
              val b = max(fromPos, toPos)
              for (i in a..b) {
                  var holder = mRecyclerView.findViewHolderForAdapterPosition(i) as? EffectsHolder
                  holder?.bindEffect(holder.effect, i)
              }
              NativeInterface.rotateEffectAt(fromPos, toPos)
          }
      })
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.stompai.kick, PID: 20883
      kotlin.TypeCastException: null cannot be cast to non-null type com.mobileer.androidfxlab.EffectsAdapter.EffectsHolder
          at com.mobileer.androidfxlab.EffectsAdapter$itemTouchHelper$1.onSwiped(EffectsAdapter.kt:213)
          at androidx.recyclerview.widget.ItemTouchHelper$4.run(ItemTouchHelper.java:712)
          at android.os.Handler.handleCallback(Handler.java:789)
          at android.os.Handler.dispatchMessage(Handler.java:98)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6617)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
  
  </code></pre>
  
  <p>You can see by the error message that it happens in the <code>itemTouchHelper.onSwiped</code>. You can see that I <strong>did do a safe cast</strong> to an <code>EffectsHolder</code>! It still gives the error! I then completely commented the function:</p>
  
  <pre><code>override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
              Log.d(TAG, "onswiped!");
              /*
              val position = viewHolder.adapterPosition
              effectList.removeAt(position)
              mRecyclerView.adapter?.notifyItemRemoved(position)
              NativeInterface.removeEffectAt(position)
  
              for (i in position until effectList.size) {
                  var holder = mRecyclerView.findViewHolderForAdapterPosition(i) as? EffectsHolder
                  holder?.bindEffect(holder.effect, i)
              }
  
              */
          }
  </code></pre>
  
  <p>But it still gives the same error! The error blames line <strong>213</strong> which is not onSwiped but it's where th eitemTouchHelper finishes. I guess the compiles it not able to detect exactly but it says onSwiped on the backtrace as you can see.</p>
  
  <p>How is it possible that it blames a null cast conversion if I'm not casting anything??</p>