-1

App closes when the button is pressed.
It has something to do with null or string/int conversion, but I can't find the error since I am VERY new to it.

import kotlinx.android.synthetic.main.activity_main.*

button.setOnClickListener() {
    if (DenaryInput.text !== null) {
        var den: Int? = DenaryInput.text.toString().toInt()
        var BinString: String? = "0"
        var remainder: Int?
        while (den!! > 0) {
            remainder = den % 2
            den = den / 2
            BinString += remainder.toString()
            textView.text = BinString?.reversed()
        }
    }
}

Here are my logs

2021-08-19 22:15:18.532 6182-6182/com.example.button E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.button, PID: 6182 java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:627) at java.lang.Integer.parseInt(Integer.java:650) ...

Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
Set GFX
  • 1
  • 1
  • Does this answer your question? [How can I prevent java.lang.NumberFormatException: For input string: "N/A"?](https://stackoverflow.com/questions/18711896/how-can-i-prevent-java-lang-numberformatexception-for-input-string-n-a) – javdromero Aug 19 '21 at 17:32
  • What don't you understand about the crash log? An empty string `""` cannot be turned into an Integer. – Tenfour04 Aug 19 '21 at 18:24

2 Answers2

0

The crash log says it all. You cannot pass an empty string and convert it as an Integer. You will need to change the input value and check to see if it is an empty string or not BEFORE you can click the button and proceed with the instructions.

DoctorWho
  • 1,044
  • 11
  • 34
0

I think that DenaryInput is edittext or textview and getting text will always return non null editable but it will return empty editable instead so trying to convert string to int will cause exception, I tried to enhance your code by adding extension function that convert string to int or default zero

public fun String.toIntOrDefault(def : Int): Int = toIntOrNull()?:0

button.setOnClickListener() {
if (DenaryInput.text !== null) {
    var den: Int = DenaryInput.text.toString().toIntOrDefault(0)
    var BinString: String? = "0"
    var remainder: Int?
    while (den > 0) {
        remainder = den % 2
        den = den / 2
        BinString += remainder.toString()
        textView.text = BinString?.reversed()
    }
}

}

and another solution is instead of checking DenaryInput is not null you can check it is not empty

if (DenaryInput.text.trim().isNotEmpty()){
    var den: Int = DenaryInput.text.toString().toInt()
    var BinString: String? = "0"
    var remainder: Int?
    while (den > 0) {
        remainder = den % 2
        den = den / 2
        BinString += remainder.toString()
        textView.text = BinString?.reversed()
    }
}

and another piece of advice is to try using data binding instead of kotlin android extension

more information about data binding

https://developer.android.com/topic/libraries/data-binding

more information about migrating to data binding or view binding

https://developer.android.com/topic/libraries/view-binding/migration

Islam Assem
  • 1,376
  • 13
  • 21