Solutions for Frequently occurred exceptions in java language
Exception handling is a fundamental aspect of Java application development, playing a crucial role in ensuring application stability and minimizing errors. When errors occur in the application, Java employs exception classes to handle them effectively. This process involves using try, catch, and finally blocks.
Developers often encounter various types of exceptions during the development phase. This post aims to provide a comprehensive list of common exceptions and their respective solutions.
Here are some commonly encountered exceptions in the Java programming language:
NullPointerException in java
A common RuntimeException encountered in Java is the NullPointerException. This occurs when a method is invoked on an object with a null reference, indicating that the object has not been created or initialized properly.
- Manipulating the state of a null object
- Invoking a method on a null object
- Performing any operation on a null object
String str=null;
System.out.println(str.toString());
The solution in our code is.
if (str == null) {
// Initialize the string object here
} else {
System.out.println(str.toString());
}
To prevent NullPointerException in Java, ensure that the object is properly initialized before accessing its methods or state. Implement condition logic to handle null checks.
ClassCastException error in java
This RuntimeException occurs when attempting to cast an object to a class that is not part of its instance. For example, in the below code, casting Integer
to String
and String
is not a child class of Integer
. and it results in java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String
Object intObject = Integer.valueOf(5793);
String str = (String)intObject;
Fix for ClassCastException Ensure that you are casting to the correct types, avoiding invalid type conversions.
java NumberFormatException error
This RuntimeException is thrown when attempting to convert a string to an integer, and the string does not contain a parsable number.
In the below code, parseInt()
method is called with a string which contains characters, and the result is java.lang.NumberFormatException: For input string: “s123”.
try {
int i = Integer.parseInt("s123");
} catch (NumberFormatException ex) {
ex.printStackTrace();
}
Fix for NumberFormatException Use try and catch handlers to handle such cases appropriately.
java SocketTimeoutException error
This RuntimeException
is thrown during HTTP communication over the socket layer between different applications.
With sockets communication between client and server. Server not accepting sockets accept/read operations or unable to respond for a specific time.
From a developer’s point of view, we don’t have control over the server-side if it is an External third-party Server. From the client side, Fix for SocketTimeoutException is configuring socket timeout and handling exceptions using try
and catch
blocks.
java OutOfMemoryException error
This RuntimeException
occurs when heap memory is full. To fix this, either increase the heap memory or optimize the code to minimize object creation. Configure JVM parameters to adjust the heap size at the server level. You can configure JVM parameters to increase heap size at the server level.