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 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)
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? = null val changeDate: CheckBox? = null val yes: RadioButton? = null val no: RadioButton? = null
and you can access them by their ids. like
date.setText("15-11-2017")
Fatal EXCEPTION:main , kotlin.KotlinNullPointerException, You shouldn't use assertion operator(!!). This is the biggest reason for the error. Instead, you can use the safe call operator(?) or Elvis operator (� Starting from Kotlin 1.4, the exception will be NullPointerException instead (a different type), see here. So if any code catches explicitly KotlinNullPointerException-- which is discouraged -- then upgrading to 1.4 will be a breaking change. – TheOperator Jan 6 at 23:34
Your EditText is null. You can initialize your EditText. Example Kotlin code,
override fun onCreate(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 } }
Fatal Exception: kotlin.KotlinNullPointerException on some devices , FATAL EXCEPTION: main KotlinNullPointerException - android. The error says something about null pointer exception. This is my first time using Kotlin and I� cezaryjurkiweicz changed the title Fatal Exception: b.b when change to night mode on Android 7 Fatal Exception: kotlin.KotlinNullPointerException when change to night mode on Android 7 May 10, 2019 cezaryjurkiweicz changed the title Fatal Exception: kotlin.KotlinNullPointerException when change to night mode on Android 7 Fatal Exception: kotlin.KotlinNullPointerException when webView (or Chrome) is old May 10, 2019
val date: EditText? = null val changeDate: CheckBox? = null val yes: RadioButton? = null val no: RadioButton? = null
Before using these variables, initialize them with findViewById
or bind them to the xml view.
FATAL EXCEPTION: main KotlinNullPointerException, E/AndroidRuntime: FATAL EXCEPTION: main. Process: com.tommy.loyote. tablet, PID: 32492 kotlin.KotlinNullPointerException Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
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
CollectPayment caused kotlin.KotlinNullPointerException � Issue , 01-04 12:02:45.251 5973-5973/com.example.ernest.myapplication E/ AndroidRuntime: FATAL EXCEPTION: main. Process: com.example.ernest. myapplication,� E/AndroidRuntime﹕ FATAL EXCEPTION: main [closed] Ask Question Asked 6 years ago. Active 6 years ago. Viewed 78k times 4. 1. Closed. This question
KotlinNullPointerException stacktrace � GitHub, February 20, 2020 Java Leave a comment. Questions: I am setting a textview as HTML retrieved from Firebase database. When I am setting the question the I am adding another value called qid to the textview.
A fatal error is a type of error that forces a computer program to close or the entire operating system to shut down suddenly. This type of error is commonly associated with the blue screen of death in Windows, but less severe fatal exception errors only cause a single program to close.
A fatal exception <YZ> has occurred at xxxx:xxxxxxxx In the above example, the YZ represents the actual processor exception; this can range from 00 to 0F. Each of these processor exceptions is explained under extended information .
Comments
- date!!.setText("15-11-2017") date you declared as null. and you are setting text to null variable. Set text after initialise the view date.