Sudhir Singh Khanger
06/26/2019, 5:21 PMval articles: List<ArticlesItem?>? = null
as part of my JSON response. I am trying to pull data off this list using articles[position]
but Android Studio complains that Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List<ArticlesItem?>?
. Why can I do newsDataSet!![position]
but not newsDataSet?[position]
?
val articlesItem: ArticlesItem? = newsDataSet[position]
full code https://github.com/sudhirkhanger/Samachar/blob/911f934252409e9a726e30a4865518e738042c93/app/src/main/java/com/sudhirkhanger/newsapp/ui/list/NewsListAdapter.kt#L45Shawn
06/26/2019, 5:23 PMnewsDataSet?.get(position)
thoughSudhir Singh Khanger
06/26/2019, 5:26 PMval articles
at the top do I have to just safe calls everywhere possible to avoid NPE.Pavlo Liapota
06/26/2019, 5:35 PMif (articles != null) {
articles[position]
}
or
articles ?: return
articles[position]
or
articles?.let {
it[position]
}
You can check that articles
is not null in any way you want 🙂