Fatal Exception: Main Kotlinnullpointerexception
This is my first time building an android application. However when i run the app on my Virtual Device, it stopped working and keeps crashing. The error says something about null p
Solution 1:
Because your date EditText is null.Before use editText initialise editText like this
var date = findViewById<View>(R.id.date) as? EditText
then set the value of date
date?.setText("15-11-2017")
date?.isEnabled = false
Solution 2:
Your EditText is null. You can initialize your EditText. Example Kotlin code,
overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_sales)
val date = findViewById<EditText>(R.id.editTextDate)
date.apply {
setText("15-11-2017")
isEnabled = false
}
}
Solution 3:
It seems like you forgot to initialize date
editText. Why not to try Kotlin Android Extension plugin. By using this plugin you don't need to initialize the views instead you can directly use them using their id. For your case:
Just add import
import kotlinx.android.synthetic.main.activity_add_sales.*
Then you can now remove your:
val date: EditText? = nullval changeDate: CheckBox? = nullval yes: RadioButton? = nullval no: RadioButton? = null
and you can access them by their ids. like
date.setText("15-11-2017")
Solution 4:
val date: EditText? = nullval changeDate: CheckBox? = nullval yes: RadioButton? = nullval no: RadioButton? = null
Before using these variables, initialize them with findViewById
or bind them to the xml view.
Post a Comment for "Fatal Exception: Main Kotlinnullpointerexception"