![]() |
Common
Java & JBuilder Errors Last updated 8/27/2002 |
Find an error that's not
listed here?
Send the exact error message to me along with a clear
description of the situation. (peterj@sctc.mnscu.edu).
| Bad Command or File Name |
| DOS can't find the Java Compiler (javac) so you have to tell DOS where it is located. You have to install the JDK from your CDROM or from the java.sun.com website. Once installed you have to change the PATH statement in the AUTOEXEC.BAT file so DOS knows where Javac and Java is located on your hard drive. |
Click here for more information: Use the Java Help Page (look in the Resources Section of the Java Learning Activities) |
|
I have the jdk installed and it is working ok (I type javac in the DOS window and it finds it ok.) But, after I compile the program with javac and then run it with java it cannot find it. |
| It
sounds like your upper/lower case is confused.
Just doing a file rename won't help either,
because even though it looks ok in windows,
the DOS section still keeps it the old way
(a bug in the Windows rename). Java uses the
DOS directory, so doesn't recognize your name
change. Try this to get the correct file name in both DOS and Windows: (1) Open up the file and save as to a dummy name like test.java (2) delete the old file (3) create a new file, paying attention to the how you type in the name, especially upper and lower case. (4) Open up the dummy file test.java and cut and paste your code into the new file. |
| Remember, all four things have
to be the same, including upper/lower case and
spelling. (1) the name of the class (in the program) (2) the name of the file that the program is saved as (3) the name that is typed in after javac (with the file extension .java) (4) the name that is typed after java (without a file extension) |
| Error Message: class whatever is public, should be declared in a file named whatever.java |
| You have named the class one thing and your file something else. |
The name of your class has to match the file name exactly. Upper/lower case matter. (So does spelling!) For example, you have the statement: public class MyClass The file for this class must be "MyClass.java" - anything else will cause an error. Keep in mind that Windows/DOS does not recognize upper/lower case very well. If you try to do a rename, the DOS side does not seem to change the character case of the filename. You must copy the file to some other name, delete the original file and then create a new file, cutting and pasting your code in from the copy you made. |
| ';' expected |
| Make certain you put a semicomma (;) on the end of each line of code. |
| Sometimes you have to check the line BEFORE the error. |
| ')' expected |
| Make sure all your { have matching } and all your ( have matching ) |
| ArrayIndexOutOfBoundsException |
| signals that an array index less than zero or greater than or equal to the array size has been used. Check your counting, did you remember the first element is zero? Did you remember that the last element is one less than the total number of elements? |
| [0, 1, 2, 3] has four elements. First element is position #0. The last element is position #3. |
| Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp |
| One of the places java tries to find your bytecode file is your current directory. So, if your bytecode file is on your floppy, change your current directory to that. (To change your directory, type A:\ and hit enter.) |
| Click here for more information: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html#2b |
| Exception in thread "main" java.lang.NoClassDefFoundError: a:/initals (or any other file name) |
| You are probably working on your C: drive and trying to tell Java to run a program on the A: drive. Java only looks in the folder it is in for the CLASS file. |
| To prevent this message change to the A: drive. (Type
A: and hit ENTER) Then type the Java command at the A:\> prompt: A:\> Java initals |
| Incorrect file name |
| A Class must be defined in a file. It must also be declared public. Make sure your class name and file name match exactly, both in spelling and capitalization. |
|
public class Example |
| Incorrect number of arguments |
| The argument is invalid or you forgot the commas between a list of arguments. |
| public class Example(int test, String testString) { } |
| Incorrect use of mathematical operators |
| You are probably missing a term (+ - / * ) in an expression. Remember that = assigns a variable with a value while == test to see if one thing is equal to another. |
|
x = 7; // x will hold the value of 7 |
| InstantiationError |
| Signals an attempt to instantiate an interface or abstract class. Interface and abstract classes are only promises. They have to be extended before a new object can be created (instantiated) in memory. They can't be treated like regular classes. |
| Invalid declaration |
| Make certain you put a semicomma (;) on the end of each line of code. (But not on the end of the name or header line of a method of class.) |
| Invalid type expression |
| Make certain you put a semicomma (;) on the end of each line of code. (But not on the end of the name or header line of a method of class.) |
| Null Pointer Error |
You tried to use an object that you haven't created yet. (This usually shows up when you import a large block of code you don't understand or if you delete a block of code that is essential somewhere else in the program.) Look for variables that you are using but have not been declared. |
| Make certain every object has a "new" to create it before you try to do anything with it. |
| Why do I have to set up a CLASSPATH as well as a PATH? Why can't I just put everything in the PATH statement? |
| The purpose of the PATH
statement is to tell DOS what folders to look in if a requested .EXE
file is not in the current directory. The purpose of the CLASSPATH is to tell JAVAC where to look for classes. In this case we are telling it to look in the current directory (the . ) as well as the root directory (the C:\ ) |
| Remember, the directory folder name must match the name of the class being imported. For example, AVI is not the same as avi to the Java Compiler. |
| After installing the JDK, how can I tell what is working and what isn't? |
| Here's
a checklist: (1) When you type PATH at the DOS prompt you see the path statement and can find the correct reference to your jdk directory. (It has to match the directory name exactly with a "/bin" added on. (2) When you type javac at the DOS prompt and hit enter you see the help information. This tells you that the compiler is found and the path statement is active. (3) Compile and run this program: class Hello { public static void main(String[ ] args) { System.out.println("Hello World!"); } // end of main() } // end of Hello If it works that means that you have the compiler working as well as the Java Runtime Environment (JRE) and the Java Virtual Machine (JVM) is working perfectly. Ok, now for that library called avi. When Java runs your program, when it comes to a class it will look in the JDK directory first. If it can't find a class by that name it will look in the folders designated by the "import" keyword. So, when you use the statement "import avi.*" you are telling Java to look in a folder (known in Java as a package) named "avi" for the classes that you are using. In this case the class named Window: Window screen = new Window("Example_1.java First, Java looks in the current directory. If it can't find the package there it looks where the CLASSPATH tells it to. If you set up the CLASSPATH in your AUTOEXEC.BAT like the book describes on the bottom of page 8 then Java will look in the current directory first and the root of C:\ next. If it can't find it, an error message will tell you that. Solution: either make certain the avi directory (with all its files, especially its .class files) is on the C:\ drive and make certain the CLASSPATH is correct using the example in the book. A second alternative is to copy the avi folder onto your floppy disk. Put it in the same folder as the files you are compiling. Then when you run the program Java will find it in the current directory. |
| My SQL statements. I noticed that everytime I try to compile, Java does not like my SQL statements. It wants me to insert a ";" after the keyword INTO. |
| You need to separate the variables
from the SQL string.
I also like to have more control over my INSERT, so I list each field name. I almost always refer to W3Schools SQL page. Its on my screen right now, as I speak. First, make the simplist SQL statement by hard-coding in
the values (notice the single quotes around character data): Get this to work first. Then exchange the hard coded value
with a variable: Notice the single quotes around the string variables. If you change the font to Courier and change to a larger font size you can see it much easier. Once I have the hard-coded version working, with a single
variable, I substitute in the variable from the array (the third cell
in the array is 'user'. Notice how the single quotes are kept inside the string.
Watch out for spaces too. You need one after INTO and before (user) but
not after the single quote VALUES (' . Why is this? Well the SQL processor strips off the double quotes " as soon as it receives the string leaving a command that designates what is character data and what is not. You have to alternate between the double and single quotes so the parser knows which goes with what. Now, don't get too excited. Go back in and add one field at a time then test the SQL. Remember that character fields have single quotes around them, numeric and boolean do not. This may seem long and tedious until you think back on the frustration you experienced not getting it to work. Small successes succeed. |
|
South
Central Technical College •
1920 Lee Boulevard • North Mankato, MN
56003 Peter K. Johnson Peter.Johnson@southcentral.edu • (507) 389-7337 |