Home > Net >  "lateinit property has not been initialized" exception when setting a list adapter
"lateinit property has not been initialized" exception when setting a list adapter

Time:08-12

class MainActivity() : AppCompatActivity() {

   lateinit var tumBurcBilgileri: ArrayList<Burc>


    override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)

         var myBaseAdapter = BurclarBaseAdapter(this, tumBurcBilgileri)
         ListBurclar.adapter = myBaseAdapter
     }
     private fun veriKaynagiHazirla(){
         tumBurcBilgileri=ArrayList<Burc>(12)

         var burclar = resources.getStringArray(R.array.Burc)
         var BurcTarihleri = resources.getStringArray(R.array.burcTarih)
         var BurcResimleri = arrayOf(R.drawable.koc1, R.drawable.boga2, R.drawable.ikizler3,
             R.drawable.yengec4, R.drawable.aslan5,
             R.drawable.basak6,   R.drawable.terazi7, R.drawable.akrep8,
             R.drawable.yay9, R.drawable.oglak10, R.drawable.kova11,
             R.drawable.balik12)
        var BüyükBurcResim = arrayOf(R.drawable.koc_buyuk1, R.drawable.boga_buyuk2,
            R.drawable.ikizler_buyuk3, R.drawable.yengec_buyuk4, R.drawable.aslan_buyuk5,
            R.drawable.basak_buyuk6, R.drawable.terazi_buyuk7, R.drawable.akrep_buyuk8, R.drawable.yay_buyuk9,
            R.drawable.oglak_buyuk10, R.drawable.kova_buyuk11, R.drawable.balik_buyuk12)

         var burcGenelÖzellikler = resources.getStringArray(R.array.burcGenelOzellikler)

         for (i in 0..11){
             var arayListeAtanacakBurc=Burc(burclar[i], BurcTarihleri[i], BurcResimleri[i], BüyükBurcResim[i],
                 burcGenelÖzellikler[i])
             tumBurcBilgileri.add(arayListeAtanacakBurc)
         }
     }
 }

Logcat Log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.burlar/com.example.burlar.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property tumBurcBilgileri has not been initialized

CodePudding user response:

You are getting the error because you have to intitialise the tumBurcBilgileri in onCreate() method as you have used lateinit for the variable. You can put this:-

tumBurcBilgileri=ArrayList<Burc>(12)

in onCreate() method

CodePudding user response:

You are trying to access a lateinit properly before initializing it. Prepare the tumBurcBilgileri ArrayList before setting the adapter.

override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
 }
 private fun veriKaynagiHazirla(){
     tumBurcBilgileri=ArrayList<Burc>(12)

     var burclar = resources.getStringArray(R.array.Burc)
     var BurcTarihleri = resources.getStringArray(R.array.burcTarih)
     var BurcResimleri = arrayOf(R.drawable.koc1, R.drawable.boga2, R.drawable.ikizler3,
         R.drawable.yengec4, R.drawable.aslan5,
         R.drawable.basak6,   R.drawable.terazi7, R.drawable.akrep8,
         R.drawable.yay9, R.drawable.oglak10, R.drawable.kova11,
         R.drawable.balik12)
    var BüyükBurcResim = arrayOf(R.drawable.koc_buyuk1, R.drawable.boga_buyuk2,
        R.drawable.ikizler_buyuk3, R.drawable.yengec_buyuk4, R.drawable.aslan_buyuk5,
        R.drawable.basak_buyuk6, R.drawable.terazi_buyuk7, R.drawable.akrep_buyuk8, R.drawable.yay_buyuk9,
        R.drawable.oglak_buyuk10, R.drawable.kova_buyuk11, R.drawable.balik_buyuk12)

     var burcGenelÖzellikler = resources.getStringArray(R.array.burcGenelOzellikler)

     for (i in 0..11){
         var arayListeAtanacakBurc=Burc(burclar[i], BurcTarihleri[i], BurcResimleri[i], BüyükBurcResim[i],
             burcGenelÖzellikler[i])
         tumBurcBilgileri.add(arayListeAtanacakBurc)
     }
     
     // set the adapter after the **tumBurcBilgileri** ArrayList is ready.
     var myBaseAdapter = BurclarBaseAdapter(this, tumBurcBilgileri)
     ListBurclar.adapter = myBaseAdapter
 }
 

CodePudding user response:

You are not calling the function in the onCreate where the initialization of the lateinit variable is called.

In a lateinit variable you have to assign a value to it before you use it


lateinit var tumBurcBilgileri: ArrayList<Burc>


    override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
         veriKaynagiHazirla() // call the function here
         var myBaseAdapter = BurclarBaseAdapter(this, tumBurcBilgileri)
         ListBurclar.adapter = myBaseAdapter
     }
  • Related