+ 1

[SOLVED] Please, could someone help! What's the problem here?

https://code.sololearn.com/cPtsN1R6bMPn/?ref=app

3rd Sep 2021, 6:28 PM
mesarthim
mesarthim - avatar
7 Respuestas
+ 5
Ipang , I think your'e wrong. The NOT-null assertion operator (!!) converts any value to a NON-null type! That's, why the interpreter can be sure, that this value never can be NULL. So if you use this: var word = readLine()!! with an empty input (=null), the interpreter throws an exception. It's not possible, but null-save for the whole prog. Or, you use it null-able like this: var word = readLine() Now you are able to make an empty input (=null) . But every time, you want to access this variable, you have to check it against NULL by your self, like this: fun main(args: Array<String>) { var word = readLine() if (word != null) { for(x in word) { println(x) } } }
3rd Sep 2021, 7:47 PM
Coding Cat
Coding Cat - avatar
+ 5
Line 2: Non-nullable input for variable <var> Add !! to make it nullable var word = readLine()!!
3rd Sep 2021, 6:33 PM
Ipang
+ 3
It works but why I must make it nullable? Ipang
3rd Sep 2021, 6:35 PM
mesarthim
mesarthim - avatar
+ 3
I saw this message before I added the !! "error: not nullable value required to call an 'iterator()"' method on for-loop range for(x in word){" I thought about just adding those !! And it turned out to be working, so I suggested you that also ...
3rd Sep 2021, 7:38 PM
Ipang
+ 3
Thank you for better insight Coding Cat 🙏
3rd Sep 2021, 7:59 PM
Ipang
+ 2
Thanks a lot Coding Cat [Mouse searching] Your code is also working :)
3rd Sep 2021, 8:04 PM
mesarthim
mesarthim - avatar