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:
- Read the first message. It points at
prntlnon line 1. The function the program wanted isprintln, with the missingirestored. - Fix that word and run again. The compiler reports the next problem:
whetheron line 4. - Compare with the declaration. Line 3 declares
weather. The wordwhetheris a real English word, which is why this slip is easy to miss, but it is not the name that was declared. Change it toweather.
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,printLnandprintlnare three different names, and only the last exists. The same holds for variables:Weatheris notweather. - Use before declaration. Inside a function or a block, a variable can be used only after the line that declares it. Printing
weatherabove itsvalline is also an unresolved reference. - Scope. A variable declared inside a block, such as the body of an
ifor 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
valfor 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.