Search code examples
androidandroid-recyclerviewandroid-arrayadapternosuchelementexception

How to deal with android Collection errors?


While developing Android, I wrote Adapter code for RecyclerView. But intermittently, the app closes abnormally. As a result of checking through Crashlytics of Firebase, the error contents were as follows.

enter image description here

It's an intermittent bug, so I don't know how to fix it.


Solution

  • You are using the kotlin extension function first, the crash report is quite clear about what's gone wrong "Collection contains no element matching the predicate". This means that the condition you check for in first.{check on of the items in the list meets this condition} is returning no values.

    How you handle this error is up to you. I would personally replace first with firstOrNull and do something appropriate if null is encountered