Common compilation errors
Objectives
- Be able to recognize and correct the most common compilation errors in code
What is compilation?
When we use javac
to compile a file, it will translate the human-readable Java code into an intermediary format, bytecode. Last week we discussed why this was useful; this week, we’ll see what the compiler is actually doing.
One duty of compilers is to check that the code has correct syntax. As a reminder, you can think of this as, in English, if we are checking to make sure a sentence is correct grammer and spelling. Did you notice the two mistakes in the previous sentence? The compiler is doing the same sort of thing, just checking against the rules of Java.
As you can see in this example, while it is possible for a human to understand what we meant to say, machines aren’t smart enough for this: if the grammar and/or spelling is wrong, your code will not compile and you will need to fix it.
Common compilation errors
Recall the code we saw last class that added two numbers together. We’ve also modified it to print out the result:
public class Hello {
public static void main(String[] args) {
int x = 3;
int y = 2;
int z = x + y;
System.out.println("This is what was stored in z: " + z);
}
}
Let’s open up the code above in the visualizer and verify that it compiles and prints out 5.
Missing a semicolon
This is an easy mistake; run the broken code here and see what the compilation error message is.
Notice the visualizer will highlight the error in yellow and provide a message. If you tried to compile this code in your terminal, the compiler would return the following error:
.\Hello.java:5: error: ';' expected
int y = 2
^
1 error
The caret symbol ^
shows the part of that line of code where the error is. Also note the compiler in the terminal gives the line number (line 5 in Hello.java) rather than highlighting the line of code in yellow. You should be prepared to understand compiler errors in the terminal, as you will not have visualizer access during your quizzes.
Fix the error in the visualizer and re-run the code to make sure it compiles.
Typos
Recall, in Java everything is case sensitive and you must declare all variables before using them. What happens if we refer to a variable we never declared (either as a typo or because we forgot its name)? Try running this broken code.
The error message tells you it cannot find a variable called zee
on line 7 in that file. This is fair, because we never declared a variable called zee
; we only declared something called z
. Fix the code so that it compiles (rename them both to be zee
or both z
).
Now take a look at this buggy code. What’s wrong with it? How would you fix it?
As you can see, it’s also possible to mis-type a built-in like int
; in this case, the computer thinks you are talking about a variable called in
that you forgot to declare. You also cannot use a reserved word as a variable; the error message here is less useful, but it does point you in the right direction by giving you a line number.
Now try changing System
to system
in the print statement. The compiler will tell you that Error: package system does not exist
even though this is just another simple typo. But, it does give you the line number and is sort of hinting at the problem (that there is something wrong on that line with system
).
Closing parentheses and braces
Sometimes, the compiler is unable to give a useful error message, or even identify the line number with the problem. Try running this broken code; what is wrong with it?
When you see this error, Error: reached end of file while parsing
, and the compiler points you to the end of the file, it often means have a missing closing curly brace. If you add in a {
on line 8, you will see the code is able to compile. Be prepared that in instances like these you’ll have to hunt down what is missing and where – it will get much more complicated when we cover if
statements next week.
Here is what the error looks like if we’re missing an opening curly brace. You can see the compiler at least gets the line number correct here, even though the error message itself isn’t useful.
What happens when you are missing a parentheses? Modify the code to remove one and recompile it. That error message was much more helpful. Now try it with the missing opening parentheses. You can see the error messages are not symmetric (but at least you get the line number). Similarly, it
Other basic syntax errors
What happens if we forget to add together the two numbers? Modify the code and find out. The error message is useless, but at least it gives us the right line number.
Why are there so many useless/incorrect error messages?
All the compilation errors above are enough information for a Java programmer who is familiar with the syntax of the language to figure out what is wrong. However, the compiler is trying to build a syntax-compliant intermediary representation of the code. In doing so, it can’t know what you meant to do, and it makes its best guess about what it would have otherwise expected to see at that point in time.
Try removing the [
on line 4. We know the error is on line 4, but what does it mean Error: <identifier> expected
? Well, on that line in between the parentheses after main
, Java was looking for String[] args
. Here, String
is a valid identifier (a variable or a type). But when we had String]
, Java knows that ]
cannot be a part of a valid identifier. So it tells you. Even though the issue was we were missing a ‘[’. At least we got a line number :-)
Next class
Next week in lab you’ll have your first live coding quiz where you will be expected to be able to fix the syntax errors above on code you have not seen before. To study for it, in addition to the notes above, check out the debugging_quiz1_PRACTICE
on the submitserver. You should be able to fix all the compilation errors in five minutes or less. However, we’ve set the full lab aside for you to complete this quiz in case you need help downloading the starter code, opening VSCode, uploading a tarball, etc. For future live coding quizzes, however, you should be comfortable with these tasks – you can practice them at home.
Otherwise, next week we’ll cover new syntax with Java conditionals: make sure to read over those notes in preparation for the 3 minute participation quiz at the start of next class.