Start learning free

LESSON 5 OF 348 · WELCOME TO KOTLIN

Syntax Errors in Kotlin: Reading Compiler Messages

Lesson Read the full explanation

TL ; DR:

  • Syntax errors are mistakes in the use of the Kotlin language, analogous to spelling or grammar mistakes in a language like English. For example:


  • Omitting quotes when working with strings:

    println("Welcome!)
    

  • Omitting parantheses when using the println function:

    println("Let's code!"
    

  • When Kotlin encounters a syntax error, it immediately stops executing your code and throws a SyntaxError instead.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing Kotlin code:


The syntax error:

Syntax errors are mistakes in the use of the Kotlin language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

Syntax error means there is something wrong with the way your program is written. Some examples:

1. Misuse of commas when declaring a string:

val greeting = 'Hey there!"

When we run this code we'll get an error: unclosed character literal.

2. Leaving out a parantheses:

println"Hey")

When we run this code we'll get an error: not a statement.


The reference error:

A reference error occurs when the Kotlin interpreter sees a word it does not recognize. Some examples:

1. Misspelling a class / function name:

prntln("Hey")

When we run this code we'll get an error: cannot find symbol: method prntln(String).

2. Using a non-existent variable / Misspelling a variable's name:

val animal = "parrot"
println(aniimal)

When we run this code we'll get an error: cannot find symbol: variable aniimal.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · WELCOME TO KOTLIN

    Syntax Errors in Kotlin, explained

    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!)
    
    1. Line 1. The call opens with ( but is closed with a semicolon. A closing parenthesis is missing. Put ) after the string.
    2. 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.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free