Comp210 Lab 13: Common Java Errors

Remember to read the error messages carefully; they really do contain information. Pay attention to the meanings of words like "field", "method", "class": the compiler is giving a very precise description of the problem.

Simple mistakes

  • Problem: When running my program, it stops in the middle and spews several lines of Unix-Speak about my files.
    Solution: Here's what your program printed:
    java.lang.ArithmeticException: / by zero
            at Random2.nextInt(Random2.java:16)
            at Judge.nextQuestion(HW12-3.java:36)
            at Host.play(Host.java:49)
            at Trivia.main(Trivia.java:20)
    
    This is actually useful information: a "/ by zero" error occurred in nextInt, which had been called by nextQuestion, which had in turn been called play, which had been called by main (whew!). The numbers refer to the line number within each file, of the function calls, so you can hand-evaluate, and try to deduce what happened.

  • No variable cdr defined in class List.
    Not every List has a cdr; only the sub-type Cons does. So the compiler only knows that something is a List, it can't be certain that it has a cdr, hence the error message. If you have something that you know is a Cons but the compiler only knows it's a List you need to cast it.

  • Attempt to reference method length in class List as an instance variable.
    Solution: include the parentheses when calling a function.
    Problem: Writing length without parentheses makes it look like a field, when really it is a function of zero arguments. So, include all zero of those arguments between parens when calling the function.

  • Method length() not found in class List.
    Solution: Declare abstract int length() inside abstract class List.
    Problem: You may have defined some method length() for sub-classes Cons and Null individually, but you also need to reassure the compiler that even if it only knows something which a List (without knowing which sub-class), length() is still a valid method.

  • Methods can't be overridden to be more private.
    Solution: put the keyword public in front of the offending method.
    Problem: Some functions, like toString, are already defined for all objects, and have been designated as public. (You never saw this happen.) You are welcome to override the default function with your own fancy version; however everybody who could call the default function also needs to be able to call your function.

    As far as we're concerned for your intro to Java, you can make all methods and fields public. You'll learn better if you take comp212.

  • In class Blah: void main(String argv[]) is not defined (when trying to run the program)
    Solution: Make sure class Blah has function declared as public static void main( String[] args ).

  • Class or interface declaration expected.
    Problem: You tried defining some code outside of a class. You can't do this. All functions must be methods, and there is no global names per se.
    Solution: move the code to inside a class definition. A function which does not implicitly belong to an instance may be declared static.

  • } expected.
    Solution: Add a } after the indicated line.
    Problem: Before writing a second method, you should finish writing the previous method. Similarly, before defining a second class, you should finish defining the previous class. Both of these require a "}" which can be easy to forget.

  • Head-scratchers

  • | class Null is an abstract class. It can't be instantiated.
    |       return new Null();
    |              ^
    
    This claim, even though class Null has not been declared abstract!
    Problem: class List was an abstract class which had a method length(). This requires that anybody extending class List must either provide that method, or else themselves be abstract.

    class Null extended class List, which shouldn't be a problem because sure enough it provides the method length(). However, a later error message stated

    | class Null must be declared abstract. It does not define int length() from class List.
    | class Null extends List {
    |       ^
    
    Solution: spell length correctly, inside class Null! Because it was misspelled, the compiler thought length() was not defined as promised, so it arbitrarily decided the entire class abstract.
  • If you come across an error you can't figure out, send mail to ian, and I'll add it to this page if it's good enough.

    Glossary

    Disclaimer:This is a beginning guide to terms the compiler might use. The definitions are not precise (i.e., wrong, once you learn about using keywords like static).

    class The generalization of a structure; it may contain functions ("methods") as well as data fields ("members"). This is just an object type, not an object itself.

    method A function which is inside a class.

    member A data field inside a class. Also called an instance variable.

    instance A value, whose type is some class. Always created with new. It is a value, which can be assigned to placeholders, etc.

    instance of If XX is a class and v a variable, then after saying v = new Blah(), we know v is a "native" instance of Blah, and v is a (non-native) instance of any class which Blah extended. The Java operator instanceof can determine this: if (v instanceof Blah) ....


    Back to Lab 13
    Back to Comp 210 Home