-2

enter image description here

In android monitor it's make this error like in the picture and I can't understand why is the error in the XML ? the code no problem with it can the picture that i added it make this error?

class MainActivity : AppCompatActivity() {

val ListOffood = ArrayList<Food>()
var adpter:foodAdapter=null!!

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name))
    ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name))
    ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name))
    ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name))
    ListOffood.add(Food("sandwis","m3 t7eneh",R.drawable.ic_stat_name))

    adpter=foodAdapter(this,ListOffood)
    graidV.adapter=adpter
}

class foodAdapter: BaseAdapter {
    var ListOffood=ArrayList<Food>()
    var context: Context?=null
    constructor(context: Context, ListOffood:ArrayList<Food>):super(){
        this.context=context
        this.ListOffood=ListOffood
    }


    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View? {
        val food = ListOffood[p0]
        var view = LayoutInflater.from(context).inflate(R.layout.foodlist,p2,false)
        view.textV.text=food.name
        view.imageV.setImageResource(food.image!!)

        return view
    }


    override fun getItem(p0: Int): Any {
        return ListOffood[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return ListOffood.size

    }


}

}
holi-java
  • 29,655
  • 7
  • 72
  • 83

2 Answers2

1

The property adapter should be use a late-initialized property or a nullable property. Otherwise, you will get a KotlinNullPointerException by the NPE-lovers operator !!, for example:

//                     v---cast `null` to any type will throws NullPointerException
var adpter:foodAdapter=null!!

Should be a late-initialized property:

lateinit var adpter:foodAdapter

OR a nullable property:

var adapter:foodAdapter? = null
holi-java
  • 29,655
  • 7
  • 72
  • 83
0

To expand on holi-java answer, Kotlin unlike Java tries to be a null safe language.

Unless explicitly defined all variables in Kotlin are not nullable. Therefore if you wish to have a variable that can hold a null value you will need to add ? after the type name to indicate this is the case.

var canBeNull: String? = null
var canNotBeNull: String = null // Won't compile
var canNotBeNull2: String = null!! // Will crash and burn at runtime

In your example you were forcing the compiler to accept the null value into your adapter variable which led to the runtime NPE.

The !! syntax should only be used when you are sure that the value will not be null but the compiler is unable to make that distinction. This can be the case when using 3rd party libraries that are written in Java.

David Pacheco
  • 429
  • 5
  • 5