Please help a newbie :pray: This allows me to crea...
# android
a
Please help a newbie 🙏 This allows me to create a value in a database (firebase) but if the editText value is left empty, my app crashes. Is there a way for me to replace the
null
value this throws when left empty with a default value like a
0
?
Copy code
val caloriesPerServingEditText = findViewById<EditText>(R.id.caloriesPerServingEditText)
            val caloriesPerServing = caloriesPerServingEditText.text.toString().toInt()

            database.child(addFoodName).setValue(FoodData(caloriesPerServing))
j
You can use if else statement
a
will it go where the variable is declared?
Copy code
val caloriesPerServing = if() else()
a
toIntOrNull() ?: 0
👀 1
j
I'd recommend reading through the kotlin Lang site so you have a better understanding of what your doing
a
Thank you, I'm still very green
Hi @jefbit sorry for the ping... I tried the following but got the same crash as before
Copy code
val caloriesPerServing = if (caloriesPerServingEditText.text.toString().toInt() > 0) {
    caloriesPerServingEditText.text.toString().toInt()
} else {
    0
}
thanks a ton for even looking at this... do you see by chance where I'm messing up?
m
Did you try the suggestion that was mentioned above ? I belive in your current context it’d look like this:
Copy code
val caloriesPerServing = caloriesPerServingEditText.text.toString().toIntOrNull() ?: 0