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)
1 ответ
+ 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! :)