Fixing Common Java Errors!

Now I will Share about Fixing Common Java Errors! Whether you are just starting your first Java program, or you are a well experienced programmer, your code can not be an error free code and compile successfully from the first time. At the least you may confuse variable names, forget to declare a variable, or miss a semicolon.

Java language errors, just like all other programming languages, can be either syntax errors or logical errors. Syntax errors, also called compile time errors, are illegal use of statements in terms of programming language rules. When the compiler catches them it generates an error message indicating the class name, line number where the error is found, and the type of error. However, sometimes the actual error may not actually be in the line indicated in the message. You would want to look in and somewhere before these lines to locate the error.

This happens because syntax errors can be tricky sometimes; one error may lead to more than one error message. A missing semicolon for example causes the following one or more lines to be invalid, and thus generates error messages for all of them. Once you fix that error, all following messages will be cleared.

When writing a program with Java, syntax errors are almost unavoidable, so do not panic or get disappointed when you encounter them. Some of these errors are very common and you are very likely to encounter one or more of them when compiling your code. The fact is, you cannot avoid them. So the best way to deal with them is to get yourself familiar with them and know how to fix them. Below are some of the most common errors along with their solutions.

Capitalization Errors: Java language is case sensitive, variable names must be exactly the same, Total is not the same as total and not the same as toTal.

The file name is different than the public class name: The class should be saved in a file with exactly the same name; a Shirt class should be saved in a file named Saving it to a different file name will give a compiling error.

Javac cannot find the class: When you save your classes, or source codes, they have to be in the very same directory where your javac.exe is. This file is responsible for compiling your codes and is created automatically when you download and successfully install the DSK machine. Generally you would install the SDK into your C: drive so there where your javac file will reside. Thus when saving your source code you need to save it in the C: directory in a folder named java.

"javac is not recognized as an internal or external command, operable program or batch file" (for Windows System) or "javac: Command not found" (for UNIX System): This means that your compiler cannot find your javac. This is due to one of two reasons, either you do not have a javac at all because you did not install the correct machine, or you did not set your PATH correctly. In this case you need to properly set your PATH, or you will have to type in the full file name path to execute it.

"Exception in thread "main" java.lang.NoClassFoundError: 'fileName': You probably misspelled the fileName, remember that java is case sensitive.

"Line nn: ';' expected": This means you moved to a new line without signaling to the compiler. When your string, or code line, is too long to fit in one line, you may want to split into two lines. Doing that just by pressing the Enter key will confuse the compiler and generate an error. Instead you will need to concatenate with a '+' sign. That is split your string into two lines, ending the first with a '+' before moving to the second line.

0 komentar:

Post a Comment