Here are listed a set of common errors that occur in the lab along with explanations of why they occur and what you can do about them.
| Error Message | Problem | |
|---|---|---|
H:\>java Progname Exception in thread "main" java.lang.NoClassDefFoundError: Progname H:\ |
It cannot find the file Progname.class. Check that the name of the
sourcefile "Progname.java" agrees with the class name in the
first line |
|
H:\>java Progname Exception in thread "main" java.lang.NoSuchMethodError: main H:\> |
This means that there is no main() method in Progname.java.or
that it contains errors. The main() method should start as public static void main(String [] args) |
|
| Compiler errors | ||
| There are a large number of potential compiler errors. Generally they have the form shown below; you try and compile a program, the compiler generates an error message, or a list of error messages. For each message it mentions the file e.g. Hello.java and the line on which the error occurred. Note that the compiler is only a program and only makes a guess as to where the error occurred. | ||
H:\>javac Hello.java
Hello.java:1: '{' expected
public class Hello
^
1 error
H:\>
|
The code was
There should have been an opening brace, i.e. {. The first part of the error message Hello.java:1: indicates that the error occured in the first line of the file Hello.java. The actual error is '{' expected. The offending line then follows and the caret, i.e. ^, indicates where in the line that the error occurred. |
|
| H:\>javac Progname.java error Java.21 variable varname may not have been initialized
|
This can happen when varname has been assigned a value in
an if statement with no default option so there is a chance that it will never be assigned
a value if none of the conditions are true. If this is a multiple condition
statement, check if you need all the ifs or if the last
one should be the default option. If you cannot change the if statement you will
need to initialise the variable to a value where you first declare it. e.g. int varname =
0; or in the case of a String :-
String varname = ""; |
|
| H:\>javac Progname.java progname.java:9: attempt to reference field lenght in a int[] for(i = 0; i < scores.lenght; i++) ^ |
incorrect spelling of length |
| H:\>java Progname Exception in thread "main" java. lang.ArrayIndex OutOfBounds Exception at arrayname.main (Compiled code) | Reason using a subscript with a value outside the range of the array. Example if your array is defined as size 10 it can be indexed with 0-9 for(i = arrayname.length;i >=0; i--) System.out.println(arrayname[i]); would cause an error as i would have 10 in it correction start at length-1 |
| Symptoms: Array out of bounds , caused by empty statement. | Reason: for(i=0;i<arrayname.length; i++); <<< System.out.print(arrayname[i]); System.out.println(); Semi colon after closing bracket - nothing happens in loop apart from i incrementing. It comes out of loop when i is equal to arrayname.length which is one higher than the index to the last slot next statement causes out of bounds exception |
Problems arising using classes |
| Symptom Errors in a class had been corrected but when the program was compiled it came up with the old version .
|
Problem The author had decided to add a number to the classname and file name to make it a different version. Example changed class Product to Product2 and constructor to match and fixed all the errors. However the test program creates an object from a statement such as Product x = new Product(arguments etc. ) so when TestProd is compiled it will look for a class Product and find Product.java and try to compile it . So it never finds the corrected version which has the wrong name. Solution When you are using a test program that has been provided don't change the class name. If you are writing both the class and the driver program yourself make sure they agree. |
H:\>