1

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

1
  • enqueue is async, there is no guarantee that onResponse will be executed before judul_headline_berita.text = judul_headline Commented May 21, 2020 at 14:28

2 Answers 2

0

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="" 
Sign up to request clarification or add additional context in comments.

3 Comments

i had been tried initialize it in onCreate but still same, any idea how using lazy?
I modified my answer. @ThoriqulUmar
My answer is modified again @ThoriqulUmar
0

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) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.