- 1

Kotlin get if Var doesn't exist

How do I know if a variable does not exist in Kotlin ? I tried if (X == Null) {print("Hello")}, but it doesn't work I need to know this for saving values in android studio. I need urgent help, or I'll get fired and die of starvation (joke).

3rd Dec 2022, 8:30 PM
belal bayrakdar
11 Answers
0
Tibor Santa The reason I don't know if it has been declared is because I am making an app, where I store a value in memory and if the user opens the app for the first time I cannot refer to it. I realised I can use an try catch statement for this
4th Dec 2022, 12:11 PM
belal bayrakdar
+ 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
+ 1
Bob_Li Bro all, of you don't understand what I mean. I am making an app where I save the user's input to local memory. The first time I can't access that variable cause I haven't created it yet, so I want to know if it exists. I will be using Sharedpreferences for the saving.
5th Dec 2022, 9:45 AM
belal bayrakdar
0
Ausgrindtube No that is not my whole code. If I already stored the variable I want to edit it, else set it to a value I already know.
3rd Dec 2022, 8:44 PM
belal bayrakdar
0
Ausgrindtube Why did you use a semi colon when initializing x ? Do you even know Kotlin ?
3rd Dec 2022, 8:47 PM
belal bayrakdar
0
Ausgrindtube My problem isn't sutuation specific. It's a language question.
3rd Dec 2022, 9:16 PM
belal bayrakdar
- 1
Tibor Santa Your wisdom is requested and greatly appreciated.
3rd Dec 2022, 8:31 PM
belal bayrakdar