Why errors happen
People write programs, and people make mistakes. Kotlin checks your code before running any of it, and when something is wrong it stops and reports an error message that points at the problem. Learning to read those messages is one of the most useful early skills. Two kinds of error show up all the time: syntax errors and unresolved references.
Kotlin is a compiled language. The compiler reads the whole program first. If it finds an error, the program doesn't start at all, so none of its output appears, not even from the lines before the mistake. This differs from languages that run line by line until they hit a problem.
Syntax errors
A syntax error is a mistake in how the code is written, like a spelling or grammar mistake in English. The sentence "Would you some tea?" is missing a verb, and a reader can tell something is off. In the same way, the compiler can't make sense of code whose structure is broken.
The most common causes are missing or extra punctuation.
A missing closing quote
println("Welcome!)
The string opens with " but never closes. The compiler reads the closing parenthesis as part of the text and reaches the end of the line still inside the string. It reports messages such as syntax error: Expecting '"'. and syntax error: Expecting ')'.
A missing closing parenthesis
println("Let's code!"
The string is fine, but the call to println is never closed. The compiler reports syntax error: Expecting ')'. The apostrophe in Let's causes no trouble, because inside double quotes it is an ordinary character.
Mixed quotes
val greeting = 'Hey there!"
In Kotlin, single quotes are for a single character. A string that opens with ' and closes with " is neither a character nor a string, and the compiler reports incorrect character literal.
A missing parenthesis in the call
println"Hey")
The opening parenthesis is missing. The compiler reports function invocation 'println()' expected, together with syntax error: Unexpected tokens (use ';' to separate expressions on the same line).
Unresolved references
The second common error is not about punctuation. The compiler sees a name it doesn't know, and it reports an unresolved reference. Typos cause most of them.
A misspelled function name
prntln("Hey")
The compiler reports unresolved reference 'prntln'. Nothing with that name exists. The same happens with the wrong case: Println("hi") is an unresolved reference, because Kotlin is case-sensitive.
A misspelled variable name
val animal = "parrot"
println(aniimal)
The variable is called animal, but the code asks for aniimal. The compiler reports unresolved reference 'aniimal'. When you see this message, check the spelling and the capital letters of the name it quotes.
How to read an error message
- Read the first message first. One mistake can cause several messages, as the missing quote above does. Fix the first and run again before worrying about the rest.
- Use the position. A message comes with a line and column. The mistake is often on that line, but a missing closing quote or parenthesis can be reported a little later than where you made the mistake, so look at the line above too.
- Look for pairs. Every
(needs a), every{needs a}, and every opening quote needs a closing one. - Read the quoted name. For unresolved references, the message names what the compiler couldn't find.
The exercise
The lesson gives two lines, each with a small syntax error. The goal is a program that prints Hello! and I love coding!.
println("Hello!";
println("I love coding!)
- Line 1. The call opens with
(but is closed with a semicolon. A closing parenthesis is missing. Put)after the string. - Line 3. The string starts with
"and never closes. Put the closing"after the exclamation mark and before the parenthesis.
The fixed program is:
println("Hello!")
println("I love coding!")
The blank line between the two statements does nothing, and neither does the missing semicolon: Kotlin ends a statement at the end of the line, so semicolons are optional.
Key takeaways
- The Kotlin compiler checks the code first, and a program with an error doesn't run at all.
- A syntax error is a mistake in how the code is written, such as a missing quote or parenthesis.
- An unresolved reference means a name, such as a function or variable, doesn't exist: check spelling and capitalization.
- Read the first error message first, and look at the lines around the reported position.
- Every opening quote, parenthesis and brace needs its partner.