Home > database >  lateinit property dataName has not been initialized
lateinit property dataName has not been initialized

Time:03-18

i don't know what happened with this

Main Activity

private lateinit var adapter: Adapter
private lateinit var newRecyclerView: RecyclerView
private lateinit var dataAvatar: Array<Int>
private lateinit var dataUsername: Array<String>
private lateinit var dataName: Array<String>
private lateinit var dataLocation: Array<String>
private lateinit var dataRepository: Array<String>
private lateinit var dataCompany: Array<String>
private lateinit var dataFollowers: Array<String>
private lateinit var dataFollowing: Array<String>
private lateinit var users: ArrayList<Data>


override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    newRecyclerView = findViewById(R.id.Rc_list)
    newRecyclerView.layoutManager = LinearLayoutManager(this)
    newRecyclerView.setHasFixedSize(true)

    users = arrayListOf<Data>()
    getUserdata()

}

private fun getUserdata() {
    for (i in dataName.indices) {


        val user = Data(
            dataAvatar[i],
            dataName[i],
            dataCompany[i],
            dataLocation[i],
            dataRepository[i],
            dataFollowers[i],
            dataFollowing[i],
            dataUsername[i]
        )
       /*user.avatar = dataAvatar[i]
        user.name = dataName[i]
        user.company = dataCompany[i]
        user.location = dataLocation[i]
        */

        users.add(user)
    }
    val adapter = Adapter(users)
    newRecyclerView.adapter = adapter
    adapter.setOnItemClickListener(listener = object : Adapter.onItemClickListener {
        override fun onItemClick(position: Int) {

            //  Toast.makeText(this@MainActivity, users[position].name, Toast.LENGTH_SHORT).show()

            val intent = Intent(this@MainActivity, Detail::class.java)
            intent.putExtra("avatar", users[position].avatar)
            intent.putExtra("username", users[position].username)
            intent.putExtra("name", users[position].name)
            intent.putExtra("location", users[position].location)
            intent.putExtra("repository", users[position].repository)
            intent.putExtra("company", users[position].company)
            intent.putExtra("followers", users[position].followers)
            intent.putExtra("following", users[position].following)
            startActivity(intent)




            [email protected](intent)
            Toast.makeText(this@MainActivity, users[position].name, Toast.LENGTH_SHORT).show()
        }
    })

}

and i got this problem

2022-03-18 09:18:27.393 13874-13874/com.example.githubuser2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.githubuser2, PID: 13874 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.githubuser2/com.example.githubuser2.MainActivity}: kotlin.UninitializedPropertyAccessException: lateinit property dataName has not been initialized at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dataName has not been initialized at com.example.githubuser2.MainActivity.getUserdata(MainActivity.kt:100) at com.example.githubuser2.MainActivity.onCreate(MainActivity.kt:40) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)  2022-03-18 09:18:27.469 13874-13874/com.example.githubuser2 I/Process: Sending signal. PID: 13874 SIG: 9

launch is succesfull on android studio, but the app it's not opening.. it's just force close before i can see the first activity

CodePudding user response:

accessing the lateinit variable before initialize it. you must lateinit variable initialized before using it.

error point is here

private fun getUserdata() {
    for (i in dataName.indices) {  // << ErrorPoint dataName.indices (dataName is not initialized but try to access it)
        ...
    }
}

you can check initialized or not by isInitislized

if(::dataName.isInitialized){
    for (i in dataName.indices) {
        // ... do something
    }
}

CodePudding user response:

lateinit property dataName has not been initialized

This is because your're accessing the dataName variable before initializig it with values in

val user = Data(
            dataAvatar[i],
            dataName[i], <<<<
            dataCompany[i],
            dataLocation[i],
            dataRepository[i],
            dataFollowers[i],
            dataFollowing[i],
            dataUsername[i]
        )

for private lateinit var dataName: Array<String>.

populate the array with data and then access it.

CodePudding user response:

You are using dataName without initializing

Try

private var dataName: ArrayList<String> = ArrayList()

or you can check at runtime if lateinit is initialized or not by

lateinit var dataName:  Array<String>

if(this::dataName.isInitialized) { // Do your work} 
else {//dataName is not initialized }
  • Related