在可空接收器类型的arraylist上仅允许安全或非null断言调用 [英] Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

查看:96
本文介绍了在可空接收器类型的arraylist上仅允许安全或非null断言调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用kotlin进行android开发.我的arraylist声明如下-

Just started using kotlin for android development.My arraylist is declared like this-

var day1: ArrayList<DietPlanDetailModel>? = null

现在我正在尝试按元素的位置访问元素

Now I am trying to access an element by its position

    val dietPlan= day1[position]

但是我低于编译时错误-

but i am getting below compile time error-

在可为null的对象上仅允许安全或非null声明的呼叫 数组列表的接收者类型

Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

为什么会出现此错误,我该如何解决?

Why am i getting this error and how can i resolve it?

推荐答案

问题是,您将ArrayList定义为nullable.您在这里有两个选择:

The problem is, that you defined the ArrayList as nullable. You have two options here:

1)不要定义变量nullable(这取决于您的代码): var day1: ArrayList<DietPlanDetailModel> = ArrayList()

1) don't define the variable nullable (this depends on your code): var day1: ArrayList<DietPlanDetailModel> = ArrayList()

2)使用空检查访问您的数据结构: val dietPlan= day1?.get(position)

2) access your data-structure with a null check: val dietPlan= day1?.get(position)

这篇关于在可空接收器类型的arraylist上仅允许安全或非null断言调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆