Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6
Your question is confusing. "If a variable doesn't exist" It doesn't exist only if you have never declared it, so you cannot refer to it as a variable. If you mean, it's never been initialized, then I recommend to read this about null safety. https://kotlinlang.org/docs/null-safety.html Kotlin has been designed with great effort to avoid null pointer exceptions, which are very troublesome in Java. Most kotlin-native types are not allowed to have null value. If you need to use null, then you can take advantage of safe call operator (?.) and Elvis operator (?:) to make the code more idiomatic. And you can compare to null with the equals operator but remember that null is lowercase!
3rd Dec 2022, 10:50 PM
Tibor Santa
Tibor Santa - avatar
+ 5
I've not met Kotlin personally but I have written some codes in the language. Maybe it's an "old habit", same with JavaScript. All I can say is that without your whole code it's hard to guess what's going wrong.
3rd Dec 2022, 8:57 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
belal bayrakdar maybe you are confusing value with variable. Variables are defined when you are writing the code. The user can only assign value to them. So what you should be doing is to check what the value of the variable is. The user sets the variable, not declare it. If it is stored, then you check the memory location for the value.
5th Dec 2022, 5:36 AM
Bob_Li
Bob_Li - avatar
+ 2
Is that your whole code? You probably need to declare the variable properly first. var x; Or var x = (whatever you want)
3rd Dec 2022, 8:43 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
you know it doesn't exist if you can't access it. So create a default value for it. Something like a placeholder value that you can use. Or define it and Kotlin will assign it a default null value. without specific code, it is hard to explain. You're thinking about it the wrong way. in your original question you said you tried if(X==Null){println("hello")} Kotlin uses null, not Null. also, that means you have a variable X.(capital letters for variables is not conventional, suggest use x) you are not looking for a variable, only checking what value it contains. simplest example fun main() { var name = readLine() if(name!=null) println("Hello "+name) else println("Hello stranger") } It will print Hello stranger if there is no user input.
5th Dec 2022, 10:05 AM
Bob_Li
Bob_Li - avatar