Java 10 common exceptions, causes and solutions

 

Top 10 common java exceptions, causes and solutions 
As a JAVA programmer, it is inevitable to have bugs when writing code. Let's take a look at the top 10 common java exceptions 

NO.1 java.lang.NullPointerException 

  This exception must be encountered frequently by everyone. The explanation is "the program encounters a null pointer", which simply means that an uninitialized object or a non-existing object is called. This error often occurs in operations such as creating pictures and calling arrays, such as uninitialized pictures. Or the wrong path when the image was created, etc. Null pointers appear in array operations. In many cases, it is a common mistake made by friends who have just started learning programming, that is, confusing the initialization of arrays with the initialization of array elements. The initialization of the array is to allocate the required space for the array, and the elements of the initialized array are not instantiated and are still empty, so each element needs to be initialized (if it is to be called) 

NO.2 java .lang.ClassNotFoundException 

  This exception is a common problem for many programmers who originally developed in JB and other development environments, and compiles packages under JB under WTk. The explanation of the exception is "the specified class does not exist". Consider whether the name and path of the class are correct. If the package is made under JB, it is usually added with Package by default, so after going to WTK, pay attention to adding the path of Package. 


NO.3 java.lang.ArithmeticException 

  The explanation of this exception is "mathematical operation exception". For example, if there is an operation such as dividing by zero in the program, such an exception will occur. For this kind of exception, you should check your own program. Where mathematical operations are involved, is there something wrong with the formula? 

NO.4 java.lang.ArrayIndexOutOfBoundsException 

  I believe many friends have often encountered this exception. The explanation of the exception is "array subscript out of bounds". Now most programs have operations on arrays, so when calling arrays, you must carefully check and see the subscript you call. Is it beyond the scope of the array? Generally speaking, it is not easy to make such mistakes in explicit (that is, directly subscripting with constants) calls, but implicit (that is, using variables to indicate subscripting) calls often make mistakes, and there is another The situation is that the length of the array defined in the program is determined by some specific methods, not declared in advance. At this time, it is best to check the length of the array first to avoid this exception. 

NO.5 java.lang.IllegalArgumentException 

  The explanation of this exception is "method parameter error". Many methods in J2ME class libraries will cause such errors in some cases. For example, if the volume parameter in the volume adjustment method is written as a negative number, This exception will occur, for example, the three values ​​in the method g.setColor(int red, int green, int blue), if there are more than 255, this exception will also occur, so once we find this exception, what we need to do, It is to quickly check whether there is an error in the parameter transfer in the method call. 

NO.6 java.lang.IllegalAccessException 

  The explanation of this exception is "no access rights". This exception occurs when the application wants to call a class, but the current method does not have access rights to the class. Pay attention to this exception when using Package in the program. 

NO.7 java.lang.IncompatibleClassChangeError 

Incompatible class change error. Thrown when an incompatible change has occurred to the class definition on which the method being executed depends on. Generally, this error is easily caused when the declaration definition of some classes in the application is modified without recompiling the entire application and running directly. 

NO.8 java.lang.InstantiationError 

instantiation error. This exception is thrown when an application tries to construct an abstract class or interface through Java's new operator. 

NO.9 java.lang.LinkageError 

linking error. This error and all its subclasses indicate that a class depends on other classes, and after the class is compiled, the dependent class changes its class definition without recompiling all the classes, thereby causing an error condition. 

NO.10 java.lang.StackOverflowError 

stack overflow error. This error is thrown when an application is too deep in recursive calls to overflow the stack.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326882831&siteId=291194637