for (i in position until effectList.size) {
var holder = mRecyclerView.findViewHolderForAdapterPosition(i) as EffectsHolder
holder.bindEffect(holder.effect, i)
}
My code is causing a null pointer cast like this:
kotlin.TypeCastException: null cannot be cast to non-null type com.mobileer.androidfxlab.EffectsAdapter.EffectsHolder
Because mRecyclerView.findViewHolderForAdapterPosition(i)
returns null. How can I conditionally cast only if mRecyclerView.findViewHolderForAdapterPosition(i)
is not null? Then I can do holder?.bindEffect(holder.effect, i)
Kotlin allows you to perform a cast like this using the Safe (nullable) Cast Operator:
val maybeString: String? = someObject as? String
So in your case, perhaps something like this:
var holder = mRecyclerView.findViewHolderForAdapterPosition(i) as? EffectsHolder
holder?.bindEffect(holder?.effect, i)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With