1

I just switched to Kotlin from Java. Doesn't matter what language is, I don't want mess in my onCreate function. Therefore, I initialize my UI objects in a function. In Java, I was able to do that. I was declaring objects in global, and initializing them in my define function. But when I tried the same way to do that in Kotlin, I'm getting the following error:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property send has not been initialized
        at com.example.kotlinalistirma.MainActivity.action(MainActivity.kt:24)
        at com.example.kotlinalistirma.MainActivity.define(MainActivity.kt:34)
        at com.example.kotlinalistirma.MainActivity.onCreate(MainActivity.kt:19)

Here's my MainActivity.kt:

class MainActivity : AppCompatActivity() {

    lateinit var send: Button
    lateinit var name : EditText
    lateinit var soyisim : EditText
    lateinit var textView : TextView

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


    fun action(){
        send.setOnClickListener{
            textView.append(name.text.toString() + "\n")
            textView.append(soyisim.text)
        }
    }
    fun define(){
        val send : Button = findViewById(R.id.sendButton)
        val name : EditText = findViewById(R.id.name)
        val surname : EditText = findViewById(R.id.surname)
        val textView : TextView = findViewById(R.id.textView)
        action()
    }

}

And activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="name"
        android:id="@+id/name"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="surname"
        android:id="@+id/surname"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/sendButton"
        android:text="send"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textSize="30dp"/>

</LinearLayout>
1
  • Consider accepting an answer to close this question. Commented Jun 28, 2021 at 11:46

2 Answers 2

2
val send : Button = findViewById(R.id.sendButton)
...

this creates a new, local variable inside that function (the one called define) - it doesn't touch the outer variables you declared at the top

remove the val

    fun define() {
     send  = findViewById(R.id.sendButton)
     name  = findViewById(R.id.name)
     surname = findViewById(R.id.surname)
     textView = findViewById(R.id.textView)
     action()
}
Sign up to request clarification or add additional context in comments.

3 Comments

Oh ok. I just got it. When I type val, it's like another "Button send" in Java, and that makes it a new object right? Thanks.
yes, what you're doing is creating two variables with the same name, one local and one global if you're using val, because you're declaring them as variables within the scope of your function
@muratbulut it doesn't make it a new object, it just creates a local variable where the reference is stored instead of on the property of the object.
1

The problem is that you are creating a new variables in the define method

fun define(){
        val send : Button = findViewById(R.id.sendButton)
        val name : EditText = findViewById(R.id.name)
        val surname : EditText = findViewById(R.id.surname)
        val textView : TextView = findViewById(R.id.textView)
        action()
    }

If you have lateinit var you need to use these variables so your define method should look like :

fun define(){
        send = findViewById(R.id.sendButton)
        name = findViewById(R.id.name)
        surname = findViewById(R.id.surname)
        textView = findViewById(R.id.textView)
        action()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.