Java 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. 


something expected
The parser was surprised by a symbol you wrote at or just before this point.

name is abstract; cannot be instantiated
You are trying to construct an instance of an interface or abstract class. You can't do that. Did you mean to construct something else instead?

cannot be dereferenced
You are calling a method on a primitive.  For example:  42.doStuff()

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 have the wrong argument types, or you called it on the wrong file/class.

cannot find symbol:  variable
You got the variable name wrong, or you forgot to declare the variable, or you meant to call a method but forgot parentheses.

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.

method(...) cannot be applied to (...)
The arguments you are passing to the method do not match the types of that method's parameters.

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, found value
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)


My program freezes.
You have a loop that never reaches its stopping condition.  Adding print statements can help you find your infinite loop.

ArrayIndexOutOfBoundsException
You tried to access an array element with an index that was too high or too low.

ConcurrentModificationException
You are using a for-each loop to loop over a data structure, and you are modifying the data structure inside your for-each loop.

NullPointerException
Short answer:  You wrote
null.
or
null[
Long answer:  You can do lots of things with null values--store them in variables, pass them to methods, return them from methods, etc.  The only things you cannot write are null. and null[
Therefore, you should 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 theclimbOneStair 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!

Please show me the error message and I'll be happy to add it to this page.