Start learning free

LESSON 6 OF 414 · WELCOME TO SCALA

Name Errors in Scala: Fixing "Not found" Compiler Errors

Lesson Read the full explanation

TL ; DR:

  • A name error (reported as Not found) occurs when Scala sees a word it does not recognize. Some examples:

  • Misspelling a function name:

    prntln("Heyooo!") // Error: Not found: prntln
    

  • Misspelling a variable name:

    var animal = "parrot"
    println(anmal) // Error: Not found: anmal
    

  • When Scala encounters a name error, it reports a compile error and your program does not run at all.




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 Scala code:


The syntax error:

Syntax errors are mistakes in the use of the Scala 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. Mismatched quotes when declaring a string:

var greeting = 'Hey there!"

When we run this code we'll get an error such as unclosed string literal.

2. Leaving out a parenthesis:

println"Hey")

When we run this code we'll get a syntax error, because the closing parenthesis has no opening one.


The reference error:

A reference error occurs when the Scala compiler 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 the error Not found: prntln.

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

var animal = "parrot"
println(aniimal)

When we run this code we'll get the error Not found: 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

    SCALA · WELCOME TO SCALA

    Name Errors in Scala, explained

    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 whether instead of weather;
    • a different capital letter: Println and println are 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

    1. Start with the first error. The compiler points at prntln on line 1 and suggests println.
    2. Correct the function name. Change prntln to println. Nothing else on the line changes.
    3. Move to the second error. The compiler points at whether on line 4.
    4. Look at what was defined. Line 3 creates a value called weather. The name in println has to match it letter for letter.
    5. 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 named weather whose type is String. The name after val is the one every later use has to repeat exactly.
    • Names are case sensitive, so Weather, WEATHER and weather are three different names.
    • println is 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 found error 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.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free