Interpreting Compile-time Error Messages

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > Back to Chapter Topics


Introduction

Let's face it: debugging---interpreting error messages and incorrect output and trying to fix your code---sucks. It can be hard and frustrating. Worse yet, error messages provided by the Java compiler can often be cryptic and don't appear to be very helpful. This section will describe how to read java compile-time errors, how to interpret what they mean, and provide some possible leads on what could be wrong with your program.

   

{{{Body}}}

Note: This article is for reference only. You will not be tested on any of the material on this page. It is only provided to you for help in debugging your programs.

Preface: How to read Java Compile-Time Error Messages

Here is a typical Java error message displayed at compile-time:

foobar.java:2: class fooBar is public, should be declared in a file named fooBar.java
public class fooBar {
       ^


This error tells us that the problem is located in the file "foobar.java" on line 2, and then gives us a short description of the error: "class fooBar is public.." This should hopefully give an idea of where the problem is (java might be wrong, though). The next line tries to give an indication of where in line 2 the problem is: java says that the "class fooBar" portion is the problem.

In Textpad, clicking on the error will take you back to the line in your code where java thinks the error is.


When all else fails: add System.out.println() statements to print out the values of the variables around where trouble is happening -- these can give you an idea of what's going wrong in your program.

Error Using Java

  • NoClassDefFound: see this posting to solve this problem (technically, a run-time error, but it is common enough to deal with here!).
  • Usage: javac <options> <source files>: this is usually a problem when either
    • your file is not saved as a .java file. To solve this, resave the file with the .java extension.
    • in Textpad, if you click on "Compile" while you are looking at the output window (say of a previous run), then you will get this error. Just make sure you have your java file displayed when you click on Compile.

Errors while setting up programs

  • class XXX is public, should be declared in a file named XXX.java: your class name (after public class) should match the filename exactly, including upper and lower case. To solve this, resave the file with the proper file name.
  • class or interface expected: if this line refers to the start of your program, it may be because public class is not entered correctly. Make sure your file starts with public class and then the classname (see Common Syntax Errors below for another cause of this error).
  • missing method body, or declare abstract: in your declaration of main (public static void main (String[] args)) make sure you have an opening brace ({) next, and not a semicolon.
  • package XXX does not exist: this typically happens when using an import statement like import javax.swing.*; but you have a spelling error or a case (upper/lower) problem.

Common Syntax Errors

  • ';' expected: your code is missing a semicolon at the end of a statement. Be careful: java often flags the next line after the one that's missing the semicolon. Check previous lines for missing semicolons.
  • '}' expected: your code is probably missing a closing brace somewhere. Make sure all your opening braces, including main, other methods, ifs, elses, and loops, are all balanced.
  • 'class' or 'interface' expected: when this error occurs near the bottom of your file, it's likely caused by too many closing braces. Make sure all your braces are balanced.
  • not a statement: this error can be caused by trying to use a java keyword word as a variable name. Here is a list of java's keywords.
  • <identifier> expected: this is usually caused by trying to declare a variable without providing a name, such as
    final double = 3.5;
    

    To solve this, make sure when you declare a variable, the name is provided on the left-hand side of the assignment.

Common JOptionPane Errors

  • cannot find symbol: In relation to JOptionPane, this can be caused by more than one problem:
    • if the next line reads symbol  : variable JOptionPane, this usually means you forgot the import javax.swing.* line in your program.
    • if the next line reads symbol  : method showMessageDialog(java.lang.String), this usually means you forgot to put null, before the String you want to print

Type Errors

In the following, XXX and YYY stand for types, like int, double, char, or String (but String will read java.lang.String).

  • possible loss of precision
    found : double
    required: int

    This is usually caused by a statement where you are trying to assign a double expression (like 3.5) to a integer variable. This will happen even if the double value is a whole number, but has a decimal place, like 3.0. To solve this problem, use casting.

  • incompatible types 
    found: XXX
    required: YYY
    

    This error can occur when you have an assignment statement that tries to assign an expression of one type (usually XXX) to a variable of an incompatible type (usually YYY). Examples include trying to assign boolean

    expression to pretty much anything else.
  • incomparable types: XXX and YYY: in an if statement, if you try to compare two types that can be compared (e.g., int and boolean), you will get this error.
  • variable Z might not have been initialized: you have declared the variable Z, but you haven't given it a value before using it in an expression. Be warned: sometimes (especially with loops) you might be sure that Z gets a value before it is ever used, regardless of how the loop runs, but java is not so sure. To get rid of this error, you can give Z a dummy value when it is declared (one that you are sure that will never be assigned to Z later).
  • operator Z cannot be applied to XXX,YYY: the operator Z (e.g., +,&gt, etc.) cannot be applied to the combination of an expression of type XXX and an expression of type YYY. Your expression needs to be rewritten so that both sides of the operator Z have comparable types.
  • XXX cannot be derferenced: variables of primitive types, like int and char cannot be accessed with methods like .length(). So if myInt is of type int, an expression like myInt.length() will cause this error.

Errors with ifs

  • 'else' without 'if': this error can be caused by not having proper braces around if and else statements. Make sure all open braces { are matched by a closing brace }. This can also be caused by a semicolon after an if statement. If you have something like
    if (x==y);
    {
      //code
    }
    

    or something like

    if (x==y)
      line 1;
      line 2;
    else
    

    (notice there are no braces in the 2nd example), this will cause the error.

  • illegal start of expression: when referring to a brace on an else or else if clause, this can be caused by a missing opening brace on the previous if or else if statement.
  • ( expected: in if statements, the boolean expression must be surrounded by parentheses.

Weird Errors

  • Illegal character errors: these are usually caused by transferring source files (.java files) between machines of different types (Windows/Mac/Linux) or by copying and pasting code from an HTML page. The best way to solve this problem is to retype the code in a new file.