A null return from readLine()
indicates an end-of-file condition — basically, there can be no more input.
But whether and how that can happen depends on how you're running the program:
If you're running it in a terminal window, then you can signal end-of-file with a special key combination, which depends on the platform:
- On macOS and Linux:
Ctrl
+D
- On Windows:
Ctrl
+X
then Return
If you're running it with the input redirected from a file (e.g. by appending <filename
to the command), then you'll get an end-of-file condition after reading all the content of the file.
Some IDEs may provide their own combinations. For example:
- IntelliJ on macOS:
Cmd
+D
. (Ctrl
+D
starts the debugger instead.)
- IntelliJ on Windows:
Ctrl
+D
.
Some IDEs may provide no way at all.
Some IDEs (e.g. online) don't support user input, and so readLine()
will always return null.
Due to the confusion that this can cause, Kotlin 1.6 added a readln()
function, which never returns null: instead, it throws a RuntimeException
if end-of-file is reached, or on platforms such as Kotlin/JS which don't support user input in this way.
(That may make it slightly easier to write beginner programs, but IMHO it's merely turning a compile-time issue into a hidden run-time one, which seems like a backward step for a language as clear and safe as Kotlin…)