Errors

Don't take errors personally. Even experts spend much of their time finding errors in their programs.

This page only tells you what your error message means--not how to fix it. That will depend on what you intended your code to do. Don't attempt to fix your code until you understand why it's broken.

Compile-Time Errors (Syntax Errors)

Don't pay any attention to the number of errors. Just read the first error message and work on fixing that error. (Every now and then, the second error message will help you fix the first error.)

These errors are often caused by very small mistakes that are easy to miss, so there's no shame in having someone else help you find the mistake.

Error MessageWhat It Usually Means
something expected The parser was surprised by a symbol you wrote at or just before this point.
cannot find symbol -- class Make sure that file is saved in the same folder as the file that refers to it.
cannot find symbol -- method You got the method name wrong, or you called it on the wrong file/class.
class, interface, or enum expected You have too many closing braces.
class is public, should be declared in a file named Your class name and file name must match exactly.
illegal start of expression You're missing a closing brace for the previous method declaration.
illegal start of type You wrote a statement that does not appear inside a method body.
incompatible types -- expected type Make sure you understand why it found what it did, and why it expected what it did.
missing method body Your method declaration line has a semicolon.
missing return statement The compiler found a pathway through your non-void method that does not reach a return statement.
non-static method cannot be referenced from a static context You called a method on a class name, instead of on a specific instance of that class.
possible loss of precision You assigned a double value to an int variable.
reached end of file while parsing You're missing a closing brace at the end of the file.
unexpected type -- required variable You used = instead of ==.
unreachable statement You wrote this statement after a return statement. Remember that return statements return from the method immediately.
variable might not have been initialized The compiler found a pathway through your method where you access the value of a variable before you've assigned anything to it.

Run-Time Errors (Crashes)

Error MessageWhat It Usually Means
My program freezes. You have a loop that never reaches its stopping condition.
ArrayIndexOutOfBoundsException You tried to access an array element with an index that was too high or too low.
NullPointerException Look for every period (.) or open bracket ([) on the line of code that caused the error. Something immediately to the left of one of these periods/brackets must have been the null value (instead of being the object or array you thought you had).
OutOfMemoryError You are constructing new objects inside an infinite loop.
StackOverflowError You have an infinite recursion. In other words, your method calls itself, which then calls itself, without ever stopping.
StringIndexOutOfBoundsException You tried to access a character in a String with an index that was too high or too low.

How To Read A Run-Time Error

Suppose our program crashes with the following error message:
java.lang.RuntimeException: Attempt to move robot from (4, 2) to occupied location (4, 3)
          at Robot.move(Robot.java:80)
          at Lesson.climbOneStair(Lesson.java:13)
          at Lesson.climbAllStairs(Lesson.java:7)
Clearly, this error message is telling us that we told the robot to move into a wall (and it even tells us where the robot and the wall are), but there's a lot more we can learn from reading the rest of the message. Run-time error messages should be read from the bottom up. According to this error message, we called the climbAllStairs method, which, on line 7 of Lesson.java, called the climbOneStair method, which, on line 13 of Lesson.java, called the move method, which crashed on line 80 of Robot.java, when the robot tried to move into a wall.

Now, we might guess that the bug is in move, since that's where the program crashed. But we probably trust that the move method has been carefully tested, so we must have called move in an inappropriate manner. Specifically, we must have violated the move method's precondition, by calling move when the front of the robot was blocked. So, we back up to line 13 of Lesson.java, where we called the move method. Now we need to step back and think. Was climbOneStair correct in calling move? If not, we should fix the bug in climbOneStair But if climbOneStair was correct, maybe we called climbOneStair when we shouldn't have, and the bug is in climbAllStairs ...

Help! My Error Doesn't Appear On This Page!

Copy and paste the complete error message into an email and send it to me, along with a copy of the offending code, and a description of what you were doing when you got this error. That way, I can add the error message to this page.