Ivan Herrera
Ivan Herrera

Reputation: 1

java.lang.IllegalStateException: nicknameTextView must not be null

I´m trying to do this AboutMe example, but the application keeps crashing and send the error message: java.lang.IllegalStateException: nicknameTextView must not be null.

I don't understand why nicknameTextView always result null. I added the println command to see the result from editText and it works. I know it has something to do with null value and initialize the variable somehow by reading other posts, however I don't get it.

Thanks.

package com.example.aboutme

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<Button>(R.id.done_button).setOnClickListener(View.OnClickListener {
            addNickname(it)
        })
    }
    private fun addNickname(it: View?) { // funcion para añadir el nick tecleado
        val editText = findViewById<EditText>(R.id.nickname)    // Lee el Nick Tecleado
        val nicknameTextView =
            findViewById<TextView>(R.id.nickname_text)   //Define el texto donde se va a escribir el nick tecledo

        println(editText.text)
        nicknameTextView.text = editText.text
        editText.visibility = View.GONE
        done_button.visibility = View.GONE
        nicknameTextView.visibility = View.VISIBLE
    }
}

MAIN ACTIVITY

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding"
    android:textAlignment="viewStart">

    <TextView
        android:id="@+id/name_text"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/nickname"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/What_is_your_nickname"
        android:inputType="textPersonName"
        android:textAlignment="center" />

    <TextView
        android:id="@+id/nickname_text"
        style="@style/NameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="none"
        android:textAlignment="center"
        android:visibility="gone" />

    <Button
        android:id="@+id/done_button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/layout_margin"
        android:fontFamily="@font/roboto"
        android:text="@string/done" />

    <ImageView
        android:id="@+id/star_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_margin"
        android:contentDescription="@string/yellow_star"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/Imagen1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:contentDescription="@string/Imagen1"
                app:srcCompat="@android:drawable/ic_menu_view" />

            <TextView
                android:id="@+id/bio_text"
                style="@style/NameStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingMultiplier="1.2"
                android:text="@string/bio_name" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Upvotes: 0

Views: 726

Answers (1)

waseefakhtar
waseefakhtar

Reputation: 1423

Instead of initializing it every time the button is clicked, why don't you initialize it once in onCreate? For instance:

private lateinit var editText: EditText
private lateinit var nicknameTextView: TextView

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

    editText = findViewById(...)
    nicknameTextView = findViewById(...)
   
    ...
}

And then just call the variables whenever needed.

Upvotes: 1

Related Questions