1. Syntax Errors (Syntax Errors)
Error Type:Compilation time errorreason:The code does not comply with the syntax rules of the Java language.
Example 1:
public class Main { public static void main(String[] args) { ("Hello, World!"); // Missing the close brace}
error message:
error: expected '}' at the end of file
Solution:
Make sure each bracket appears in pairs and there must be closed braces at the end of the code block.}
。
2. NullPointerException
Error Type:Runtime errorreason:Access or operate one fornull
Object of .
Example 2:
public class Main { public static void main(String[] args) { String str = null; (()); // Null pointer exception } }
error message:
Exception in thread "main"
Solution:
Before operating the object, make sure that the object has been initialized and notnull
。
3. ArrayIndexOutOfBoundsException
Error Type:Runtime errorreason:Access elements that do not exist in the array.
Example 3:
public class Main { public static void main(String[] args) { int[] arr = new int[3]; arr[5] = 10; // Array subscripts cross boundaries } }
error message:
Exception in thread "main" : Index 5 out of bounds for length 3
Solution:
Make sure that the subscript of the access array is within the valid range and avoid crossing the bounds.
4. Type conversion exception (ClassCastException)
Error Type:Runtime errorreason:Illegal object type conversion.
Example 4:
public class Main { public static void main(String[] args) { Object obj = new String("Hello"); Integer num = (Integer) obj; // Type conversion exception } }
error message:
Exception in thread "main" : class cannot be cast to class
Solution:
Ensure that the source object is compatible with the target type when performing type conversion.
5. FileNotFoundException
Error Type:Runtime errorreason:Try to access a file that does not exist.
Example 5:
import .*; public class Main { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("nonexistent_file.txt"); // file not found } }
error message:
Exception in thread "main" : nonexistent_file.txt (No such file or directory)
Solution:
Make sure the file path is correct, the file exists, or use exception handling to avoid program crashes.
6. Divided by zero exception (ArithmeticException)
Error Type:Runtime errorreason:When the divisor is zero, a divisor exception is thrown.
Example 6:
public class Main { public static void main(String[] args) { int result = 10 / 0; // Divide by zero exception } }
error message:
Exception in thread "main" : / by zero
Solution:
Before division operation, make sure that the divisor is not zero.
7. IllegalThreadStateException
Error Type:Runtime errorreason:Try to perform unauthorized operations on the thread when it is in an illegal state.
Example 7:
public class Main { public static void main(String[] args) { Thread t = new Thread(() -> ("Hello")); (); (); // Repeat the thread } }
error message:
Exception in thread "main"
Solution:
Make sure that the thread has not started again if it has been started.
8. Method Undefined Exception (NoSuchMethodException)
Error Type:Runtime errorreason:Try to call a method that does not exist.
Example 8:
public class Main { public static void main(String[] args) throws NoSuchMethodException { Class<?> clazz = ; ("nonExistentMethod"); // The method has no exception defined } }
error message:
Exception in thread "main" : ()
Solution:
Make sure the method called is correctly defined in the target class.
9. Deadlock (Deadlock)
Error Type:Runtime errorreason:Two or more threads wait for each other to release resources, resulting in the program being unable to continue execution.
Example 9:
public class Main { private static final Object lock1 = new Object(); private static final Object lock2 = new Object(); public static void main(String[] args) { Thread t1 = new Thread(() -> { synchronized (lock1) { ("Thread 1 holding lock 1..."); try { (100); } catch (InterruptedException e) {} synchronized (lock2) { ("Thread 1 holding lock 2..."); } } }); Thread t2 = new Thread(() -> { synchronized (lock2) { ("Thread 2 holding lock 2..."); try { (100); } catch (InterruptedException e) {} synchronized (lock1) { ("Thread 2 holding lock 1..."); } } }); (); (); } }
error message:The program will not terminate and will fall into a deadlock state.
Solution:
Avoid waiting between threads and adopt appropriate lock management strategies, such as usingReentrantLock
orLock
oftryLock
method.
10. ClassNotFoundException
Error Type:Runtime errorreason:The Java program cannot find the required class when it is running.
Example 10:
public class Main { public static void main(String[] args) throws ClassNotFoundException { (""); // The class did not find the exception } }
error message:
Exception in thread "main" :
Solution:
Make sure the classpath is correct, or the class exists when using reflection.
The above are some common error types and examples in Java programming. During the development process, when encountering these errors, you can refer to the error information for debugging and repairing.
Summarize
This is the end of this article about the summary of common java errors and solutions. For more relevant java errors, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!