Java Methods Home Page Skylight Publishing



Java Methods A & AB

Object-Oriented Programming and Data Structures

Appendix B: Common Syntax Error Messages


C:\mywork>java hello

Exception in thread "main" java.lang.NoClassDefFoundError: hello (wrong name: Hello)

This run-time error (exception) happens when you mistype a lower case letter for upper case. Normally a class name (e.g., Hello) starts with an upper case letter and the file name should be the same. Under Windows, the command

javac hello.java
will compile the file Hello.java, but when you try to run it, as above, it reports an exception. It should be:
C:\mywork>java Hello


C:\mywork>java Hello.java

Exception in thread "main" java.lang.NoClassDefFoundError: Hello/java

or

C:\mywork>java Hello.class

Exception in thread "main" java.lang.NoClassDefFoundError: Hello/class

The command to run the Java interpreter should use the class name but should not include any extension, neither .java nor .class. An extension in the file name confuses the interpreter about the location of the class (the extension is interpreted as a subfolder).


C:\mywork>java Hello

Exception in thread "main" java.lang.NoSuchMethodError: main

This exception may be reported when the main method is missing or its signature is incorrect. The correct signature is

public static void main (String[] args)
Possible mistakes:
private static void main (String[] args)
public void main (String[] args)
public static int main (String[] args)
public static void main (String args)


C:\mywork>javac Test.java

class Hello is public, should be declared in a file named Hello.java
public class Hello
       ^

The source file name is different from the name of the class defined in the file. Here the file name is Test and the class name is Hello. They must be the same.


cannot return a value from method whose result type is void
    return 0;
           ^

In Java, main is void, not int, so return is not needed and you can't use return 0 in it.


non-static method printMsg(java.lang.String) cannot be 
referenced from a static context
      printMsg(s);
      ^

The method printMsg is called directly from main, without any dot-prefix, and the keyword static is missing in the printMsg header:

  public void printMsg(String msg)
should be:
  public static void printMsg(String msg)
Since main is a static method and it calls printMsg with no "something-dot" prefix, printMsg is assumed to be another static method of the same class.


cannot find symbol
symbol  : class EasySound
location: class Hello
    EasySound console = new EasySound();
    ^

Unless installed as a package and properly imported, files for classes used in the program (in this case EasySound.java or EasySound.class) should be available in the same folder as Hello.java.

This error message may also show up when a library class is not imported. For example:

cannot find symbol
symbol  : class Scanner
location: class Hello
    Scanner kboard = new Scanner(System.in);
                         ^
You need
import java.util.Scanner;
at the top of your program.

Another possible reason for this error message is incorrect or misspelled primitive data type name. For example:

  private bool match(String word, int row, int col, int rowStep, int colStep)
gives
cannot find symbol
symbol  : class bool
location: class WordSearch
  private bool match(String word, int row, int col, int rowStep, int colStep)
          ^
It should be boolean.


cannot find symbol
symbol  : method PrintMsg(java.lang.String)
location: class Hello
    PrintMsg(s);
    ^

This error occurs when a method is called incorrectly: either its name is misspelled (or upper-lower case is misplaced), or a method is called with wrong types of parameters, or a method is called for a wrong type of object or a wrong class. For example, the same error, will be reported if you write

  System.println("Hello");
instead of
  System.out.println("Hello");
Another example:
cannot find symbol
symbol  : method println(java.lang.String,java.lang.String)
location: class java.io.PrintStream
      System.out.println("You entered: ", msg);
                ^
Here a comma is used instead of a + in the println call. This makes it a call with two parameters instead of one and no println method exists that takes two String parameters.


cannot find symbol
symbol  : variable i
location: class java.lang.String
    for (i = 0; i < n; i++))
         ^

A very common error, cannot find symbol, may result from an undeclared variable or a misspelled local variable or field name, or missing parentheses in a method call. Here it should be

for (int i = 0; i < n; i++)


'}' expected
  }
   ^

An extra opening brace or a missing closing brace may produce several errors, including

illegal start of expression
  public static void printMsg(String msg)
  ^
';' expected
  }
   ^
and finally
'}' expected
}
 ^
which may be reported at the end of the file.


'class' or 'interface' expected
}
^

This error often results from an extra closing brace (or a missing opening brace).


illegal character: \8220
    System.out.println(ôHello Worldö);
                       ^

"Smart quote" characters accidentally left in the source file by a word processor instead of straight single or double quotes may cause this error. The same error is reported when the source file contains any non-ASCII character in the code (outside comments).


<identifier> expected
  static x;
          ^

<identifier> expected is a rather common error message. Here x is a variable, but the compiler thinks it is a class name. It is the data type designation that's actually missing. It should be:

  static <someType> x;
The same happens here:
  private myRows, myCols;
It gives an error:
<identifier> expected
  private myRows, myCols;
                ^
thinking that myRows is a data type. Same here:
  public static void printMsg(msg)
  {
    ...
  }
- a missing type designator (e.g., String) in a method's header produces four rather obscure errors:
<identifier> expected
  public static void printMsg(msg)
                                 ^
')' expected
  }
   ^
cannot find symbol
symbol  : class msg
location: class Hello
  public static void printMsg(msg)
                              ^
missing method body, or declare abstract
  public static void printMsg(msg)
                     ^
4 errors
It should be:
  public static void printMsg(String msg)


'(' or '[' expected
    EasyReader inp = new EasyReader;
                                   ^

Should be:

    EasyReader inp = new EasyReader();


variable inp might not have been initialized
    s = inp.readLine();
        ^

This error happens if you use a local variable before initializing it.

    EasyReader inp;
declares a variable but you need to initialize it with new.


')' expected
    System.out.print(Enter a message: ");
                           ^
unclosed string literal
    System.out.print(Enter a message: ");
                                      ^
cannot find symbol
symbol  : variable Enter
location: class Hello
    System.out.print(Enter a message: ");
                     ^
3 errors

A missing opening double quote in a literal string produces these three errors.


missing return statement
  {
  ^

A method, other than void, must return a value.


';' expected
      System.out.println("Message: " + msg)
                                           ^

A few compiler error messages are actually self-explanatory.


incompatible types
found   : int
required: boolean
        if (i = n)
            ^

It is supposed to be

    if (i == n)
Single = makes it assignment operator. It returns an int value that can't be tested in if. Similarly,
    return row = 0 && row < myRows && col >= 0 && col < myCols;
gives two errors:
incompatible types
found   : int
required: boolean
    return row = 0 && row < myRows && col >= 0 && col < myCols;
           ^
operator && cannot be applied to int,boolean
    return row = 0 && row < myRows && col >= 0 && col < myCols;
                          ^
2 errors
An extraneous space between ! and = in an != operator may give several errors, including "incompatible types":
')' expected
    if (s ! = null)
          ^
illegal start of expression
  }
  ^
';' expected
    }
     ^
incompatible types
found   : java.lang.String
required: boolean
    if (s ! = null)
        ^
4 errors
Another situation with "incompatible types" is when a literal string is used in place of a char constant or vice-versa. For example:
incompatible types
found   : java.lang.String
required: char
          grid[r][c] = "*";
                       ^
Should be
          grid[r][c] = '*';


'[' expected
    grid = new char(rows, cols);
                   ^

An array should be created using brackets, not parentheses.


array required, but java.lang.String found
          grid[r][c] = Character.toUpperCase(letters[i]);
                                                    ^

Use charAt(i) method, not [i] with strings.


possible loss of precision
found   : double
required: int
    x = 3.5;
        ^

This happens when a double value is assigned to an int variable.


'.class' expected
    double y = double(x);
                     ^
unexpected type
required: value
found   : class
    double y = double(x);
               ^
2 errors

Incorrect cast syntax causes this error. Should be

    double y = (double)x;


actionPerformed(java.awt.event.ActionEvent) in Test
cannot implement actionPerformed(java.awt.event.ActionEvent) in
java.awt.event.ActionListener;
attempting to assign weaker access privileges; was public

public class Test extends JApplet
       ^
1 error
This error is reported when the keyword public is missing in the actionPerformed method's header.


call to super must be first statement in constructor
    super("Hello");
         ^
1 error
This error is reported when the call super is not the first statement in a constructor or if it is in a method. In particular, this happens when you accidentally put void in a constructor's header.


invalid method declaration; return type required
  public hello()   // Constructor
         ^
1 error
This error is reported when a constructor's name is misspelled or is different from the name of the class. The compiler then thinks it is a method with a missing return type. It can also happen if indeed a return type is missing in a method header.


Hello is not abstract and does not override abstract method compareTo(Hello) in java.lang.Comparable
public class Hello extends JFrame
       ^
1 error
This error is reported when a class implements an interface (in this case Comparable) but does not supply all the necessary methods or misspells a method name, or has the the wrong number or types of parameters in one of the interface methods.



Skylight Publishing
support@skylit.com