What is the meaning of Error..Whats wrong in my code..Kotlin | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the meaning of Error..Whats wrong in my code..Kotlin

Code fun main(args: Array<String>){ var a: Int= readLine ()!! .toInt() var fact=1 while(a<=1){ a-- if(a>=1){ fact=fact*a } else{ println("Enter correct no.") } } println(fact) } Error: Exception in thread "main" java.lang.NullPointerException at FileKt.main (File.kt:2) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (:-2) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (:-1)

13th Oct 2022, 12:52 PM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
1 Answer
+ 4
Your code is very difficult to read. It is always better to save it on the code playground, and insert a link in your question. I reformatted and fixed your code, please check. https://code.sololearn.com/c7ve0zGr17pN/?ref=app - there must be no whitespace between readline and the parentheses, because it is a function invocation - changed the relational operator in your while loop condition, otherwise the loop wouldn't run even once. - the if condition inside the loop is redundant. If you want to validate the input, better to do it before starting the loop. - the variable a should be decremented only after the multiplication Proper formatting, indentation, and whitespaces around operators, gives much easier readability. Write tidy and clean code! :)
13th Oct 2022, 3:11 PM
Tibor Santa
Tibor Santa - avatar