Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The.

Similar presentations


Presentation on theme: "CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The."— Presentation transcript:

1 CS116 COMPILER ERRORS George Koutsogiannakis 1

2 How to work with compiler Errors The Compiler provide error messages to help you debug your code. The error message will give you the line number where the code fails the compiler. Sometimes the code in the line number given is not the real cause of the problem and we have to search further. Start by working the very first message at the top. Sometimes correcting that can take care of a number of compiler errors. 2

3 Examples of messages and what to look for 3

4 In the previous slide there are 3 errors. The reason the library class Scanner is not recognized by the compiler is because the import statement for the class was missing i.e import java. Util. Scanner Correcting this will take care of two of the 3 errors. The third error is a result of not having declared the variable register. Using a variable without declaring it will cause the compiler to indicate that it can’t find the symbol. 4

5 Examples of messages and what to look for 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. In other words maybe your class was not compiled successfully. Another possibility is that you are in the wrong directory with your command to interpret. In other words your.class file is somewhere else in your directory structure. 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. 5

6 Examples of messages and what to look for 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. 6

7 Examples of messages and what to look for 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. 7

8 Examples of messages and what to look for 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. 8

9 Examples of messages and what to look for 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. 9

10 Examples of messages and what to look for 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. 10

11 Examples of messages and what to look for 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. 11

12 Examples of messages and what to look for 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.) 12

13 Examples of messages and what to look for There are many other compiler errors. – The java tutorial provides a listing of some of the errors. Some error messages appear after the file is successfully compiled. Those are called runtime errors. 13

14 Examples of Runtime Error messages A good paper on this topic can be found at the site: http://introcs.cs.princeton.edu/java/11cheatshe et/errors.pdf This is a pdf file that you can download and use as reference 14


Download ppt "CS116 COMPILER ERRORS George Koutsogiannakis 1. How to work with compiler Errors The Compiler provide error messages to help you debug your code. The."

Similar presentations


Ads by Google