Thoriqul Umar
Thoriqul Umar

Reputation: 35

lateinit property has not been initialized when get data from array list

I want to get the first index from arraylist data that i got from API using retrofit

c

lass BeritaActivity : AppCompatActivity() {

    var listData : ArrayList<Berita> = ArrayList()
    lateinit var adapter: BeritaAdapter

    lateinit var image_headline: String
    lateinit var judul_headline: String

    companion object {
        const val JUDUL = "judul"
        const val BERITA = "berita"
        const val IMAGE = "image"
    }

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



        recycler_berita.setHasFixedSize(true)
        recycler_berita.layoutManager = LinearLayoutManager(applicationContext)

        ApiService.create()
            .getBerita()
            .enqueue(object : retrofit2.Callback<List<Berita>>{
                override fun onFailure(call: Call<List<Berita>>, t: Throwable) {
                    toast(t.message.toString())
                }

                override fun onResponse(
                    call: Call<List<Berita>>,
                    response: Response<List<Berita>>
                ) {
                    listData.addAll(response.body() as ArrayList<Berita>)
                    judul_headline = listData[0].namaKegiatan.toString()
                    image_headline = listData[0].foto.toString()
                    Log.d("Headline Judul",judul_headline)
                    Log.d("Headline Photo",image_headline)
                    adapter = BeritaAdapter(listData, this@BeritaActivity){
                        startActivity<DetailBeritaActivity>(
                            JUDUL to it.namaKegiatan,
                            BERITA to it.deskripsiKegiatan,
                            IMAGE to it.foto
                        )
                    }

                    recycler_berita.adapter = adapter
                }

            })


        judul_headline_berita.text =  judul_headline
        Glide.with(this)
            .load("https://tahfidzta.doaqu.or.id/kegiatan/"+image_headline)
            .into(image_headline_berita)
    }
}

and got this error

2020-05-21 21:10:31.309 1726-1726/com.thor.prototype E/AndroidRuntime: FATAL EXCEPTION: main Process: com.thor.prototype, PID: 1726 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thor.prototype/com.thor.doaqu.BeritaActivity}: kotlin.UninitializedPropertyAccessException: lateinit property judul_headline has not been initialized at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property judul_headline has not been initialized at com.thor.doaqu.BeritaActivity.onCreate(BeritaActivity.kt:74) at android.app.Activity.performCreate(Activity.java:7224) at android.app.Activity.performCreate(Activity.java:7213) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:201)  at android.app.ActivityThread.main(ActivityThread.java:6810)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

I dunno whats wrong with it, please help me Thank you

Upvotes: 1

Views: 12246

Answers (2)

Alex Lysun
Alex Lysun

Reputation: 506

It happened because enqueue() works asynchronously. That's mean this bloc of code

judul_headline_berita.text =  judul_headline
Glide.with(this)
    .load("https://tahfidzta.doaqu.or.id/kegiatan/"+image_headline)
    .into(image_headline_berita)

will be execute first and here image_headline isn't initialized. So solution is:

 override fun onResponse(
                call: Call<List<Berita>>,
                response: Response<List<Berita>>
            ) {
                listData.addAll(response.body() as ArrayList<Berita>)
                judul_headline = listData[0].namaKegiatan.toString()
                image_headline = listData[0].foto.toString()
                Log.d("Headline Judul",judul_headline)
                Log.d("Headline Photo",image_headline)
                adapter = BeritaAdapter(listData, this@BeritaActivity){
                    startActivity<DetailBeritaActivity>(
                        JUDUL to it.namaKegiatan,
                        BERITA to it.deskripsiKegiatan,
                        IMAGE to it.foto
                    )
                }

                recycler_berita.adapter = adapter

                judul_headline_berita.text =  judul_headline
                Glide.with(this)
               .load("https://tahfidzta.doaqu.or.id/kegiatan/"+image_headline)
               .into(image_headline_berita)
            }

Upvotes: 0

MMG
MMG

Reputation: 3268

You should initialize lateinit variable in onCreate function or using lazy. See here: https://stackoverflow.com/a/61414278/12478830

You can use easily instead of

lateinit var image_headline: String
    lateinit var judul_headline: String

this lines

  var image_headline=""
   var judul_headline=""

Upvotes: 0

Related Questions