Java learning -- common exceptions

1. java.lang.NullPointerException: null pointer exception

A null pointer exception is a null pointer. You have to operate it. Since it points to a null object, it cannot use the method of this object; For example, if the above s is null, you have to use the method of S.

 1 package Null pointer;
 2 import java.util.Scanner;
 3 public class Lianxi {
 4     static Scanner x=new Scanner(;
 5     private int a;
 6     public void set(int a) {
 7         this.a=a;
 8     }
 9     public static void main(String[] args) {
10         Lianxi[] a=new Lianxi[10];
12         for(int i=0;i<10;i++) {
13             a[i].set(x.nextInt());
14         }
15     }
16 }

error correction

 1 //Initialize it
 2 public static void main(String[] args) {
 3         Lianxi[] a=new Lianxi[10];
 4         for(int i=0;i<10;i++) {
 5             a[i]=new Lianxi();
 6         }
 7         for(int i=0;i<10;i++) {
 8             a[i].set(x.nextInt());
 9         }
10     }

2. ClassCastException - type cast exception.

Animal a1 = new Dog();  // 1
  Animal a2 = new Cat();   // 2
  Dog d1 = (Dog)a1;  // 3
  Dog d2 = (Dog)a2;  // 4
The code in line 3 is basically the same as the code in line 4. It literally converts an Animal to a Dog, but the code in line 4 will generate a java.lang.ClassCastException.
The reason is that you want to convert a cat (a2 this animal is a cat) into a dog, and in line 3, you want to convert a dog into a dog, so you can.
From the above example, java.lang.ClassCastException is an exception generated during forced type conversion,   Cast   Premise is   Forced type conversion can only be performed when the type of the object pointed to by the parent class reference is a subclass. If the type of the object pointed to by the parent class reference is not a subclass, a java.lang.ClassCastException exception will be generated.
Both a1 and a2 are animals, but a1 is a dog and a2 is a cat, so it is OK to convert a1 into a dog, because a1 itself is a dog and a2 is a cat, so it is wrong to convert into a dog.
If you know the specific type of the object to be accessed, you can convert it directly. That is, for the above example, if I know that the specific type of the object I want to access is cat, then I will Cat c = new Cat(); Call its methods through the new object c. However, generally, we cannot determine the specific type of object to be accessed.
If the type cannot be determined, it can be handled in the following two ways (assuming that the object is o):
1. Get the specific type through o.getClass().getName(). You can output this type through the output statement, that is, System.out.println(s.getClass().getName()); Then, specific processing is carried out according to the type.
2. Judge the type of O through the if(o instanceof type) statement
3. IllegalArgumentException - illegal parameter exception passed.
An exception thrown indicates that an illegal or incorrect parameter was passed to the method.
 1 public class ThreadPriorityDemo {
 2     public static void main(String[] args) {
 4         ThreadPriority tp1 = new ThreadPriority();
 5         ThreadPriority tp2 = new ThreadPriority();
 6         ThreadPriority tp3 = new ThreadPriority();
 8         tp1.setName("Olaf");
 9         tp2.setName("excavating machinery");
10         tp3.setName("Deppon Manager");
12         //Get default priority
13         //System.out.println(tp1.getPriority());//5
14         //System.out.println(tp2.getPriority());//5
15         //System.out.println(tp3.getPriority());//5
17         //Set priority value
18         //IllegalArgumentException:Illegal parameter exception
19         tp1.setPriority(1000);
21         //By viewing API Set the correct priority
22         // tp1.setPriority(1);
23         // tp3.setPriority(10);
25         //Start thread
26         //  tp1.start();
27         //  tp2.start();
28         //  tp3.start();
29     }
30 }

Solution: either replace the jdk or download the spring jar package conforming to the jdk again

4. ArithmeticException - arithmetic operation exception

1 public static void main(String[] args) {
2   // The integer 0 is used as the denominator and an arithmetic operation exception is reported
3   System.out.println(1 / 0);
4 }

Solution: check the code and modify the wrong expression.

5. ArrayStoreException - stores an object exception to the array that is incompatible with the declared type

 1 public ArrayList(Collection<? extends E> c) {
 2     elementData = c.toArray();
 3     if ((size = elementData.length) != 0) {
 4         // c.toArray might (incorrectly) not return Object[] (see 6260652)
 5         if (elementData.getClass() != Object[].class)
 6             elementData = Arrays.copyOf(elementData, size, Object[].class);
 7     } else {
 8         // replace with empty array.
 9         this.elementData = EMPTY_ELEMENTDATA;
10     }
11 }

Solution: put the String Object into the Object variable.

6. IndexOutOfBoundsException - subscript out of bounds exception

Solution: redefine the loop, modify the code, and add constraints

7. NegativeArraySizeException - create an array error exception with a negative size


This exception is thrown if the application attempts to create an array with a negative size.

8. NumberFormatException - number format exception

1 public static void main(String[] args) {
2   // String“ it"Convert to Integer Of course, it will report abnormal number format
3   System.out.println(Integer.parseInt("it"));
4 }

Solution: generally, there is no problem in transmitting parameters during output.

9. SecurityException - Security Exception

This is an exception caused by ASP.NET security. The possible cause is deployment   The server Configured in machine.config on or web.config   security policy Modified

Solution: my general solution is to reconfigure the server

10. Unsupported operationexception - unsupported operation exception

public class ListTest {
public static void main(String[] args) {
String[] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);

Exceptions are reported when calling the add and remove methods of the List produced by Arrays.asList(). This is caused by Arrays.asList() The returned internal class ArrayList of the city Arrays is not java.util.ArrayList. Both the internal classes ArrayList and java.util.ArrayList of the Arrays inherit AbstractList. Remove, add and other methods. The default throw UnsupportedOperationException in the AbstractList does not do any operation. java.util.ArrayList reproduces these methods, but the internal class ArrayList of the Arrays does not, so Will throw an exception.


1 public class ListTest {
2     public static void main(String[] args) {
3         String[] array = {"1","2","3","4","5"};
4         List<String> list = Arrays.asList(array);
5         List arrList = new ArrayList(list);
6         arrList.add("6");
7     }
8 }



Abstract method error. Thrown when an application attempts to call an abstract method.



Assertion error. Used to indicate the failure of an assertion.



Class circular dependency error. When initializing a class, throw this exception if circular dependency between classes is detected.



Class format error. Thrown when the Java virtual machine attempts to read a Java class from a file and detects that the contents of the file do not conform to the valid format of the class.



Error. Is the base class for all errors. It is used to identify serious program running problems. These problems usually describe abnormal conditions that should not be caught by the application.



Initializer error. Thrown when an exception occurs during the execution of a class's static initializer. A static initializer is a static statement segment directly contained in a class.



Illegal access error. This exception is thrown when an application attempts to access, modify a class's field or call its method, but violates the visibility declaration of the field or method.



Incompatible class change error. This exception is thrown when an incompatible change occurs to the class definition on which the method being executed depends. Generally, this error is easily caused when the declaration definition of some classes in the application is modified without recompiling the whole application.



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



Internal error. Used to indicate that an internal error has occurred in the Java virtual machine.



Link error. This error and all its subclasses indicate that a class depends on other classes. After the class is compiled, the dependent class changes its class definition without recompiling all classes, resulting in an error.



Class definition error not found. This error is thrown when the Java virtual machine or class loader attempts to instantiate a class and cannot find the definition of the class.



There is no error in the domain. This error is thrown when the application attempts to access or modify a domain of a class and there is no definition of the domain in the definition of the class.

At present, only these have been found and will be supplemented in the future.

Posted on Sun, 31 Oct 2021 09:07:33 -0400 by andz