prev | TOC | next

Compilation Errors

files: ASimpleSquare.java, HelloWorld.java (help)


When compiling your program using Jurtle, the compiler may find one or more mistakes in your program.  When this happens,

  1. the compilation halts
  2. the Errors tab is displayed with the appropriate error messages written out
  3. the compiled output (the .class file) is not created. 


After reading the error messages, you can double-click on an error and Jurtle will switch back to the Edit tab with the offending line highlighted.

The compiler may find two types of errors: syntax errors and semantic errors

Syntax Errors

If you make a typing mistake while entering or editing your program, the compiler may print out a syntax error.  This happens when something you typed doesn't conform to the rules of the Java language.  For example, the language specifies that each statement in Java must be terminated by a semicolon.  If in Lesson 2 we forgot the semicolon on the statement that prints "Hello World!", we might see the following error message when compiling:

HelloWorld.java:13: ';' expected
        Console.println("Hello world")
                                      ^
1 error

This error states that the problem was found on line 13 and that a ';' was expected but not found.

Another common syntax error is misspelling the name of a variable or method.  For example, if you wrote printline rather than  println in the above code, you would see the following upon compiling:

HelloWorld.java:13: cannot resolve symbol

symbol  : method printline  (java.lang.String)
location: class com.otherwise.jurtle.Console
        Console.printline("Hello world");
               ^
1 error

An error message saying it couldn't resolve a symbol usually means that you misspelled or misremembered a variable name, method name, or a keyword.

Semantic Errors

Even though your program may be syntactically correct, the compiler may discover a semantic error (i.e., an error in usage).  One example would be if your program tried to use a variable that has never had an initial value set.  For example, if you had the following code snippet:

public void runTurtle()
{
    int j;
    Console.println(j);
}

The compiler would complain:

Test.java:12: variable j might not have been initialized
        Console.println(j);
                        ^
1 error

Cascading Errors

Cascading errors are not a distinct category of errors like syntax and semantic errors.  Nevertheless, this is a situation worthy of some discussion.  Sometimes one error in your program will result in an ambiguity that the compiler cannot resolve.  This may result in several other error messages on the same line or later lines that may not represent true errors in the code.

For example, in the ASimpleSquare example file there is a for loop.  If the keyword for is accidentally mistyped as in:

        fo ( int i = 0; i < 4; i++ )
        {
            forward( 60 );
            right( 90 );
        }


The compiler will generate something like:

ASimpleSquare.java:24: '.class' expected
        fo ( int i = 0; i < 4; i++ )
                 ^
ASimpleSquare.java:24: ')' expected
        fo ( int i = 0; i < 4; i++ )
                      ^
ASimpleSquare.java:24: not a statement
        fo ( int i = 0; i < 4; i++ )
                          ^
ASimpleSquare.java:24: ';' expected
        fo ( int i = 0; i < 4; i++ )
                                   ^
ASimpleSquare.java:24: unexpected type
required: value
found   : class
        fo ( int i = 0; i < 4; i++ )
             ^
ASimpleSquare.java:24: cannot resolve symbol
symbol  : variable i
location: class ASimpleSquare
        fo ( int i = 0; i < 4; i++ )
                               ^
6 errors


Notice that the compiler did not even print an error saying the for keyword was in error.  In this case there was an ambiguity as to what the programmer was trying to do and it resulted in the 6 error messages.  When this happens it is best to start with the first error message and figure out what generated it.  In the above example, the first error message is somewhat nonsensical so you need to look earlier in the code to manually spot the problem.

When compiling  a class for the first time it is common to get a number of compilation errors.  Some of these will be legitimate and some are simply cascading errors.  It is always best to start with the first error message and correct that problem.  If subsequent error messages do not make sense, recompile to see if fixing the first error eliminated later ones.