Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
Sometimes, error messages can strike fear into the heart of even the bravest programmer. Fortunately some helpful, calming advice is here — advice to help you solve the problem when you see one of these messages.

NoClassDefFoundError

You get this error when you're trying to run your code. So first ask yourself, did you attempt to compile the code? If so, did you see any error messages when you compiled? If you saw error messages, look for things you can fix in your .java file. Try to fix these things, and then compile the .java file again.

If you normally keep code in the JavaPrograms directory, make sure that you're still working in this JavaPrograms directory. (In Windows, make sure that the command prompt says JavaPrograms.)

Make sure you have an appropriately named .class file in your working directory. For instance, if you're trying to run a program named MyGreatProg, look for a file named MyGreatProg.class in your working directory.

Check your classpath to make sure that it contains the .class file that you need. For example, if all your Java code is in your working directory, make sure that the classpath includes a dot.

NoSuchMethodError

When you encounter this error message, check for the misspelling or inconsistent capitalization of a method name. Check the capitalization of main (not Main).

When you issue the java command (or do whatever you normally do to run a program in your environment), does the class that you're trying to run contain its own main method? If not, then find the class with the main method and run that class instead.

Cannot Resolve Symbol

If you get an error message that includes cannot resolve symbol, check the spelling and capitalization of all identifiers and keywords. Then check again.

If the unresolved symbol is a variable, make sure that this variable's declaration is in the right place. For instance, if the variable is declared in a for loop's initialization, are you trying to use that variable outside the for loop? If the variable is declared inside a block (a pair of curly braces), are you trying to use that variable outside of the block?

Finally, look for errors in the variable's declaration. If the compiler finds errors in a variable's declaration, then the compiler can't resolve that variable name in the remainder of the code.

Expected ';' (Or Expected Something Else)

When you see an error message that says ';' expected, go through your code and make sure that each statement and each declaration ends with a semicolon. If so, then maybe the compiler's guess about a missing semicolon is incorrect. Fixing another (seemingly unrelated) error and recompiling your code may get rid of a bogus ';' expected message.

For a missing parenthesis, check the conditions of if statements and loops. Make sure each condition is enclosed in parentheses. Also, make sure that a parameter list (enclosed in parentheses) follows the name of each method.

For an expected message, check your assignment statements. Make sure that each assignment statement is inside a method. (Remember, a declaration with an initialization can be outside of a method, but each plain old assignment statement must be inside a method.)

For the 'class' or 'interface' expected message, make sure you've spelled the word class correctly. If your code has an import declaration, check the spelling and capitalization of the word import.

Missing Method Body or Declare Abstract

You get a missing method body or declare abstract message when the compiler sees a method header, but the compiler can't find the method's body. Look at the end of the method's header. If you ended the header with a semicolon, then try removing the semicolon.

If the header doesn't end with a semicolon, then check the code immediately following the header. The code immediately following the header should start with an open curly brace (the beginning of a method body). If some code comes between the header and the body's open curly brace, consider moving that code somewhere else.

An 'else' without an 'if'

Compare the number of if clauses with the number of else clauses. An if clause doesn't need to have an else clause, but each else clause must belong to an if clause.

Remember, you enclose an if condition in parentheses, but you don't put a semicolon after the condition. Did you mistakenly end an if condition with a semicolon?

Look at all the lines between an if and its else. When you find more than one statement between an if and its else, look for curly braces. If the statements between the if and its else aren't surrounded by curly braces, you may have found the culprit.

Non-static Variable Cannot Be Referenced from a Static Context

Lots of things can give you a non-static variable cannot be referenced from a static context error message. But for beginning programmers, the most common cause is having a variable that's declared outside of the main method. It's no sin to declare such a variable, but because the main method is always static, you need some special help to make the main method refer to a variable that's declared outside the main method.

The quickest solution is to put the word static in front of the variable's declaration. But first, ask yourself why this variable's declaration isn't inside the main method. If there's no good reason, then move the variable's declaration so that it's inside the main method.

FileNotFoundException (The System Cannot Find the File Specified) or EOFException

If you encounter a FileNotFoundException message, check that the file named in your code actually exists. (Look for the file using your system's explorer or using the command prompt window.) Double-check the spelling in your code against the name of the file on your hard drive.

If you've found a correctly named file on your hard drive, make sure that the file is in the correct directory. (For a program running in your working directory, a typical data file is in the working directory also.)

If you're a Windows user, make sure that the system didn't add an extra .txt extension when you created the file. (Use the command prompt window to check the file's name. Windows Explorer can hide the .txt extension, and that always leads to confusion.)

For an EOFException, you're probably trying to read more data than you have in the file. Very often, a small logic error makes your program do this. So do a careful review of all the steps in your program's execution. Look for subtle things, like improperly primed loops or the reading of array values past the array's largest index. Look for conditions that use <= when they should use <. Conditions like these can often be troublesome.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: