One of the most challenging tasks for beginners in programming is to catch bugs when errors occur.
In Java, the compiler checks for errors, but it is not always easy to deal with errors that appear there.
I have compiled a list of Java errors commonly encountered by beginners and their countermeasures in this post.
1 About Java Error Indication
Before introducing common errors in Java programming, let's look at the format displayed errors.
Many of you may not yet know what they are, so it is enough to look at them for reference. It will be helpful later.
For example, let's say you have created a program like this.
The "main" method calls the "setArray" method to set values in the "list" array.
When this program is executed, one of the errors "ArrayIndexOutOfBoundsException" will occur, introduced later.
Now let's decipher this error message.
- The first line shows what kind of error occurred. In this case, it is "ArrayIndexOutOfBoundsException.
- The second line, after "at," shows where the error occurred. In this case, it is line 8 of Java.
- The third and succeeding "at" lines also show where the error occurred. The second line shows where the error occurred, but the third and succeeding lines show where the method that caused the error was called.
In this program, the "main" method calls the "setArray()" method, so there are two layers. Still, actual programs often have a more complicated structure.
In such a case, many lines of calling classes will appear after the third line.
To analyze the error, it is necessary to use this information to analyze how the class or method that caused the error was called and which class or method has the problem.
Such error information is called a stack trace, which displays the information recorded in the memory area called the "stack" used by Java during execution.
Stack traces are indispensable for debugging and are essential information that can be useful in various ways, so keep them in mind.
2 Java Errors and Countermeasures
1 java.lang.NullPointerException
Error Description
This error occurs when an attempt is made to access a null object. It is a relatively common error during programming known as "nullpo" among engineers.
Sample Program
Execution Result
Countermeasures
- Check that instances are created, and values are set correctly.
- Debug to check when an object becomes null.
- Check whether the object is null in the program and avoid errors as much as possible.
2 java.lang.ArrayIndexOutOfBoundsException
Error Description
This error occurs when a non-existent element is specified as the index of an array and was introduced in the previous error example. This error occurs when there are only 1 to 10 indices as array indices, and the user tries to set a value to the 20th index.
Please refer to the first example for a sample program and execution results.
Countermeasures
- Check that the correct array index is specified.
- If an iterative process sets the array, check to see any problems with the number of iterations, etc.
3 java.lang.NumberFormatException
Error Description
This error occurs when an attempt is made to convert (cast) a string value to a number, but the string has an incorrect value as a number.
Sample Program
Execution Result
Countermeasures
- Check for non-numeric characters such as alphabets and symbols in the string.
- Check that there is no space or null in the string.
- Check if the string is numeric but does not exceed the range of values handled by the numeric type being converted.
4 java.lang.ClassCastException
Error Description
This error occurs when an attempt is made to cast a class to a non-convertible class.
Sample Programs
Execution Result
Countermeasures
- Check if the class of the value set in the array, etc., and the type you are trying to cast when retrieving the value match.
- Check that two classes in an inheritance relationship do not cast a superclass to a subclass.
5 java.lang.ArithmeticException
Error Description
This error occurs when incorrect arithmetic operations are performed to compute a number. For example, this error occurs when a mathematically impossible division by zero (e.g., ten divided by 0) occurs.
Sample Program
Execution Result
Countermeasures
- Check for zero division.
- If the calculation is performed on a numeric type variable, check that the number to be divided is not set to zero.
6 java.lang.NoClassDefFoundError
Error Description
It is an error that Java cannot read the class definition when calling a method or trying to create an instance.
Countermeasures
- Check the environment variable CLASSPATH as it may be incorrect.
- Check for incorrect file or package names.
- Check if the external file (jar) required for the program is loaded.
7 java.lang.OutOfMemoryError
Error Description
Java uses two memory areas, the stack area, and the heap area, at runtime. This error occurs when the memory size of the heap area required for program execution is insufficient.
The heap area is allocated for class and method definitions for object instances.
Countermeasures
- Check for infinite loops in the program.
- Check if there are any parts of the program trying to process a large amount of data at one time.
- Check for methods and variables with unnecessarily large scopes (public scope).
In the case of this error, depending on the environment, the memory size of the heap area of the JVM (Java Virtual Machine) can be checked and tuned if necessary. However, be aware that tuning may have insufficient memory for other applications.
Tuning of the JVM heap area will be introduced at another time.
3 Summary
This page has summarized the most common Java errors caught by beginners.
If you can deal with errors in this area, you should generally have no trouble programming.
I hope you will find this helpful information.
Java Book for Beginner
The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts.
It is also the best book to learn java and start your learning journey with Java Development.
Head First Java covers almost all OOPS concepts and fascinatingly explains them.
Despite several readers claiming it to be a dated book, as it covers nothing beyond Java 5.0, Head First Java is still found on the shelves of numerous Java veterans.
Thus, it is a must-have book for every Java pursuer and developer.