Chapter 1 · Foundations
Reading compiler errors
Step 1 of 17 · Concept
Everyone gets errors
Everyone writes programs that do not compile. javac's messages look strange at first, but they are precise once you can read them.
In this lesson you will:
- read javac's messages, part by part,
- fix errors one at a time,
- read a stack trace when a program crashes.
Step 2 of 17 · Concept
Two kinds of error
| kind | who finds it | what happens |
|---|---|---|
| compile error | javac, before the program runs | no .class file, nothing runs |
| exception | the JVM, while the program runs | the program stops in the middle |
Step 3 of 17 · Quiz
Did it run?
You press Run. javac prints an error and nothing else happens. Did any of your program run?
Step 4 of 17 · Demo
A missing semicolon
Run this program: it is missing something. Then read the first lines of the output.
Step 5 of 17 · Concept
Read the message from the left
- the file,
Semicolon.java, and the line,3, error, then what is wrong: a;was expected,- the line itself, with a
^, the caret, under the place where javac noticed the problem.
Step 6 of 17 · Concept
Where the caret points
The caret marks where javac noticed the problem. For a missing ;, that is right after the end of the line. Some
mistakes, such as a missing }, are noticed only lines later.
The editor marks the same place with a red squiggle. Hover over it to read the message.
Step 7 of 17 · Quiz
What does javac mean?
javac says this. What is wrong?
Pets.java:5: error: cannot find symbol
System.out.println(name + " has " + cont + " cats");
^
symbol: variable cont
Step 8 of 17 · Concept
Fix the first error first
When there are several errors, fix the first one and run again.
- javac reads from the top, and one mistake can confuse it about the lines after it.
- So later messages are often caused by the first one. They may vanish once it is fixed.
Step 9 of 17 · Exercise
Your turn: two mistakes
This program should print Ada has 3 cats. It has two mistakes. Fix them one at a time,
running after each fix.
Run it and read only the first message. Which line does it name, and what does it expect?
Line 3 needs a ; at its end. Then run again: the next message is about a name javac cannot find.
Compare cont on line 5 with the name declared on line 3.
Step 10 of 17 · Concept
Exceptions and stack traces
A program can compile and still fail while it runs. The JVM then stops it with an exception. It prints a stack trace: the kind of problem, then the methods that were running.
The program Division by zero has a method of its own, share. Read it as a named piece of code that main
calls with two numbers. It gives back the first divided by the second.
Step 11 of 17 · Demo
Division by zero
Run it. The program crashes. Read the last lines of the output.
Step 12 of 17 · Concept
A stack trace is the stack
When the program crashed, share was running on top of main (lesson 2). The trace lists the frames from the top:
Crash.java:3, the division insideshare, where it failed,Crash.java:7, the line ofmainthat calledshare.
Step 13 of 17 · Quiz
Who passed the 0?
Crash stopped with this trace. share divides by people, and people was 0. Which line gave it that 0?
java.lang.ArithmeticException: / by zero
at Crash.share(Crash.java:3)
at Crash.main(Crash.java:7)
public class Crash {
static int share(int total, int people) {
return total / people;
}
public static void main(String[] args) {
System.out.println(share(10, 0));
}
}Step 14 of 17 · Exercise
Your turn: blame the caller
This program should print Ada: 90% and then Bob: 0%. It crashes after the first line.
Read the stack trace, go to the line of main it names, and fix the call there. percent itself is right.
The trace's second line names a line of main. Go there and look at the call to percent.
percent(points, max) takes the points first, then the maximum. Compare it with Ada's call, which works.
Step 15 of 17 · Check yourself
Check yourself: pizza for everyone
This program does not compile, and once it does, it crashes. Fix it so that it prints exactly:
4 people, 3 pizzas
Slices each: 6
Two problems, in order: first what javac says, then what the stack trace says.
Step 16 of 17 · Challenge · optional
Challenge: a new message
Optional. It earns the lesson's third star.
javac stops this program with a message you have not met yet. Read it and fix the program so that it prints:
Eggs each: 1.5
The message says possible lossy conversion from double to int. Lossy means something would be lost.
eggs / 2.0 is a double, 1.5 (lesson 3). An int box has no room for the .5. Give each a type that does.
Step 17 of 17 · Recap
Recap
- A compile error stops at javac: nothing runs. An exception stops the program while it runs.
- Read javac's message from the left: file, line, error, what is wrong. The caret shows where javac noticed.
- Fix the first error first, then run again.
- A stack trace is the stack: the top line is where it failed, the next its caller.
Next: lesson 5 makes programs decide and repeat.
Review
3 questions from this lesson, in a new order each time. Answer from memory: 2 of 3 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
public class Semicolon {
public static void main(String[] args) {
System.out.println("Almost there")
}
}
public class Pets {
public static void main(String[] args) {
int count = 3
String name = "Ada";
System.out.println(name + " has " + cont + " cats");
}
}
public class Crash {
static int share(int total, int people) {
return total / people;
}
public static void main(String[] args) {
System.out.println(share(10, 0));
}
}
public class Grades {
static int percent(int points, int max) {
return points * 100 / max;
}
public static void main(String[] args) {
int max = 20;
int ada = 18;
int bob = 0;
System.out.println("Ada: " + percent(ada, max) + "%");
System.out.println("Bob: " + percent(max, bob) + "%");
}
}
public class Party {
static int slices(int pizzas, int people) {
return pizzas * 8 / people;
}
public static void main(String[] args) {
int pizzas = 3;
int adults = 2;
int children = 2;
int people = adults - children;
System.out.print(people + " people, ");
System.out.printn(pizzas + " pizzas");
int each = slices(pizzas, people);
System.out.println("Slices each: " + each);
}
}
public class Recipe {
public static void main(String[] args) {
int eggs = 3;
int each = eggs / 2.0;
System.out.println("Eggs each: " + each);
}
}