0

can someone explain can solve this problem in kotlin? Thank you very much

var weight = rweight.text.toString().toFloat() var hct = rhct.text.toString().toFloat() var EBV :Float var ABL :Float if (rman.isChecked){ EBV = weight * 75 } else if (rwoman.isChecked) { EBV = weight * 65 } ABL = EBV * (10)/hct //error in here "EBV must be initialize" 
2
  • try initializing it to something like var EBV: Float = 0.0? I think both your if and else if are not true so EBV is null in that line. Commented Jul 27, 2020 at 6:10
  • @PhaniRithvij yes, thank you very much for the answer. The problem is in my code. Commented Jul 27, 2020 at 8:03

1 Answer 1

2

You receive that error because EBV may not be initialized when it is used.

You should initialize EBV variable with default value:

var EBV: Float = 0.0f // or some other default value 

Or add else clause to the condition:

EBV = if (rman.isChecked) { weight * 75 } else if (rwoman.isChecked) { weight * 65 } else { 0.0f // assign some value to the variable } // As improvement you can replace `if` with `when` statement: EBV = when { rman.isChecked -> weight * 75 rwoman.isChecked -> weight * 65 else -> 0.0f // assign some value to the variable } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. The second method that uses "when" is work for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.