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 pointer exception. This is my first time using Kotlin and I coded in Java and changed to Kotlin.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_sales)
val date: EditText? = null
val changeDate: CheckBox? = null
val yes: RadioButton? = null
val no: RadioButton? = null
date!!.setText("15-11-2017")
date.isEnabled = false
val button2 = findViewById<View>(R.id.btnSubmit) as Button
button2.setOnClickListener {
var name = findViewById<View>(R.id.name) as EditText
var cost = findViewById<View>(R.id.cost) as EditText
val itemcost = Integer.parseInt(cost!!.text.toString())
var price = findViewById<View>(R.id.price) as EditText
val itemprice = Integer.parseInt(price!!.text.toString())
var qty = findViewById<View>(R.id.quantity) as EditText
var chgDate = false
var discount = false
val toast = Toast(applicationContext)
if (itemprice < itemcost) {
price!!.error = "Selling price cannot be lower than item price!"
} else {
if (changeDate!!.isChecked) {
chgDate = true
date.isEnabled = true
}
if (yes!!.isChecked) {
discount = true
}
}
Toast.makeText(this@AddSales, "Item Name: " + name!!.text + "\n" + "Cost: " + cost!!.text + "\n" + "Item Price: " + price!!.text + "\n" + "Quantity: " + qty!!.text + "Date: " + date + "Staff Discount: " + discount, Toast.LENGTH_SHORT).show()
}
}
and the error is:
11-17 06:23:34.603 3540-3540/com.example.ruiru.salestracker
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ruiru.salestracker, PID: 3540
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.ruiru.salestracker/com.example.ruiru.salestracker.AddSales}:
kotlin.KotlinNullPointerException
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: kotlin.KotlinNullPointerException
at com.example.ruiru.salestracker.AddSales.onCreate(AddSales.kt:25)
at android.app.Activity.performCreate(Activity.java:6975)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Best Answer
Because your date EditText is null.Before use editText initialise editText like this
then set the value of date