Start learning free

LESSON 6 OF 348 · WELCOME TO KOTLIN

Name Errors in Kotlin: Unresolved References

Lesson Read the full explanation

TL ; DR:

  • A NameError occurs when Kotlin sees a word it does not recognize. Some examples:

  • Misspelling a function name:

    prntln("Heyooo!") // NameError: name 'prntln' is not defined
    

  • Misspelling a variable name:

    val animal: String = "parrot"
    println(anmal) // NameError: name 'anmal' is not defined
    

  • When Kotlin encounters a name error, it immediately stops executing your code and throws a NameError 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: String = "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

    Name Errors in Kotlin, explained

    What a name error is

    Every name you use in a Kotlin program has to refer to something: a function, a variable, a class. When the compiler meets a name it can't connect to anything, it stops and reports an unresolved reference. People often call this a name error. Kotlin has no error type with that name; what you see is a compiler message, and it appears before the program starts, so nothing is printed at all.

    Two mistakes cause almost all of them: misspelling a function name and misspelling a variable name.

    A misspelled function

    prntln("Welcome!")
    

    The compiler reports:

    error: unresolved reference 'prntln'.
        prntln("Welcome!")
        ^^^^^^
    

    A misspelled variable

    val weather: String = "sunny"
    println(whether)
    

    Here the declaration is correct, but whether is a different word from weather, so:

    error: unresolved reference 'whether'.
        println(whether)
                ^^^^^^^
    

    The message always has the same shape: it names the exact word that could not be resolved, shows the line, and underlines the word with carets. Read the underlined word first. Usually, comparing it letter by letter with the name you meant is enough.

    Name errors versus syntax errors

    A syntax error is a different kind of mistake: the code is not written in a form Kotlin's grammar allows, much like a sentence that is missing a verb. A name error is a correctly formed line that uses a word with no meaning in the program.

    Here are two syntax errors. Mixing quote styles in a literal fails:

    val greeting = 'Hey there!"
    
    error: incorrect character literal.
    

    Single quotes in Kotlin hold exactly one character, such as 'a', and strings use double quotes. A missing parenthesis fails too:

    println"Hey")
    

    This produces more than one message, among them function invocation 'println()' expected and syntax error: Unexpected tokens. A single typo can confuse the compiler about everything after it, so the first message is usually the most useful one. Fix it, compile again, and the others often disappear.

    The difference matters when you debug. A syntax error says to look at how the line is written: quotes, parentheses, braces. A name error says to look at the spelling of one word, or at whether the name was ever declared.

    Why Kotlin catches these early

    Kotlin is a compiled language. The whole file is checked before anything runs, so a mistyped name can't hide in a rarely executed branch and surprise you later. The compiler either accepts every name in the file or refuses to build it. A misspelling therefore costs you a few seconds, not a failure in the middle of a run.

    The exercise

    The starter code has two unresolved references:

    prntln("Welcome!")        // this line produces an error
    
    val weather: String = "sunny"
    println(whether)          // this line produces an error
    

    The task is to make it print Welcome! and then sunny. Work through it the way you would for any compiler error:

    1. Read the first message. It points at prntln on line 1. The function the program wanted is println, with the missing i restored.
    2. Fix that word and run again. The compiler reports the next problem: whether on line 4.
    3. Compare with the declaration. Line 3 declares weather. The word whether is a real English word, which is why this slip is easy to miss, but it is not the name that was declared. Change it to weather.

    The corrected program:

    println("Welcome!")
    
    val weather: String = "sunny"
    println(weather)
    

    It prints:

    Welcome!
    sunny
    

    Only the two misspelled words change. The declaration, the strings and the parentheses were fine.

    Common causes and habits

    • Case matters. Println, printLn and println are three different names, and only the last exists. The same holds for variables: Weather is not weather.
    • Use before declaration. Inside a function or a block, a variable can be used only after the line that declares it. Printing weather above its val line is also an unresolved reference.
    • Scope. A variable declared inside a block, such as the body of an if or a function, isn't visible outside it. Using it there gives the same message.
    • Missing declarations. Sometimes the name isn't misspelled at all; you just never wrote val for it.
    • Let the editor help. Autocomplete types long names for you, so it can't misspell them.

    Key takeaways

    • An unresolved reference means the compiler found a name that matches nothing declared.
    • The error comes before the program runs, so nothing is printed.
    • The message names the word and underlines it: compare it with the name you intended.
    • Syntax errors concern how a line is written; name errors concern what a word refers to.
    • Fix the first error first, then compile again.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free