What a name error is
Every name in a Scala program, whether a function like println or a value like weather, has to be defined before it is used. When the compiler meets a name it cannot find anywhere, it reports a Not found error. Beginners often call this a name error or reference error. The lesson's program has two of them:
prntln("Welcome!") // this line produces an error
val weather: String = "sunny"
println(whether) // this line produces an error
On line 1 the function name println is misspelled. On line 4 the variable is spelled whether, but line 3 defined weather. The compiler has no idea what either word means.
What the compiler says
Scala checks the whole program before running any of it. If a single name is unknown, nothing runs at all, so the first Welcome! is never printed either. The compiler reports both problems together:
-- [E006] Not Found Error ------------------------------------------------
| prntln("Welcome!")
| ^^^^^^
| Not found: prntln - did you mean println?
-- [E006] Not Found Error ------------------------------------------------
| println(whether)
| ^^^^^^^
| Not found: whether - did you mean weather?
The message has three useful parts:
- the line shown with a row of
^characters under the exact word that is unknown; Not found:followed by that word;- often a suggestion,
did you mean println?, taken from the names that are in scope and look similar.
Read the suggestion first. For a typo it is usually the answer.
Two kinds of mistakes
Beginners meet two families of compile errors, and it helps to tell them apart.
Syntax errors
A syntax error means the code is not written according to the rules of the language, like an English sentence with a missing verb. A missing or extra bracket, as in println"Hey"), or a quote that is never closed, are typical causes. The compiler can't make sense of the text, so the message talks about what it expected instead, for example that a different symbol should have appeared. The exact wording depends on where the problem is.
Not-found errors
Here the code is written correctly, but a word in it points to nothing. Common causes:
- a misspelled function name, such as
prntln; - a misspelled variable name, such as
whetherinstead ofweather; - a different capital letter:
Printlnandprintlnare different names, because Scala is case sensitive; - using a name before the line that defines it, or outside the block where it was defined.
Fixing the exercise step by step
- Start with the first error. The compiler points at
prntlnon line 1 and suggestsprintln. - Correct the function name. Change
prntlntoprintln. Nothing else on the line changes. - Move to the second error. The compiler points at
whetheron line 4. - Look at what was defined. Line 3 creates a value called
weather. The name inprintlnhas to match it letter for letter. - Change the name in the call to
weather, and leave the definition on line 3 alone. If you changed the definition to match the typo instead, the program would compile, but the name would read wrongly everywhere else.
The solution in Scala
println("Welcome!") // this line produces an error
val weather: String = "sunny"
println(weather) // this line produces an error
The comments are left over from the starting code, where they described the two broken lines. They have no effect on the program. With both names corrected it prints:
Welcome!
sunny
A few details about these four lines:
val weather: String = "sunny"defines a value namedweatherwhose type isString. The name aftervalis the one every later use has to repeat exactly.- Names are case sensitive, so
Weather,WEATHERandweatherare three different names. printlnis always available without an import. It prints its argument and then starts a new line.
Common mistakes
- Fixing only one error. The compiler lists every problem it finds, but fixing one and rerunning can reveal another that was hidden behind it. Keep going until the compiler is silent.
- Changing the wrong side. When a use and a definition disagree, decide which spelling you want and fix the other one.
- Ignoring the suggestion.
did you mean ...?costs nothing to check. - Expecting partial output. A program with a compile error doesn't run its good lines. You see the error and no output.
Key takeaways
- A
Not founderror means the compiler can't find a definition for a name you used. - The cause is almost always a typo in a function or variable name, or a capital letter in the wrong place.
- Scala reports these errors before the program runs, so nothing is printed until all of them are fixed.
- Read the line, the underlined word and the suggestion, then correct the spelling.
