java.lang.NullPointerException
is a runtime exception that arises when a variable points to nothing or null and is accessed. As it is a runtime exception, there is no explicit need for handling it in the application code.
Causes of NullPointerException in Java
NullPointerException
can occur when an application code tries to access or modify an uninitialized object. This happens when the object reference has a null value and does not point anywhere. Some common scenarios that lead to NullPointerException
include calling methods on a null object, accessing null object properties, passing null parameters to a method, and incorrectly configuring dependency injection frameworks like Spring. Additionally, using synchronized
on a null object and throwing null from a method that throws an exception can also cause NullPointerException
.
Example of NullPointerException
This code demonstrates a NullPointerException
that occurs when the length()
method is called on a null String
object without checking for null values first. The printLength()
method is called from the main()
method with a null string, triggering the exception.
public class NullPointerExceptionExample {
private static void printLength(String str) {
System.out.println(str.length());
}
public static void main(String args[]) {
String myString = null;
printLength(myString);
}
}
Exception:
Exception in thread "main" java.lang.NullPointerException
at NullPointerExceptionExample.printLength(NullPointerExceptionExample.java:3)
at NullPointerExceptionExample.main(NullPointerExceptionExample.java:8)
Image: No image provided
Solution for NullPointerException
To resolve the NullPointerException
in the given example, it is necessary to check if the string has null or empty values before proceeding further. The updated code includes a check in the printLength()
method utilizing the StringUtils.isNotEmpty()
method from Apache Commons library. It first verifies that the string is not null and then calls the length()
method on the string. If the string is empty or null, it prints the message Empty string
to the console.
import org.apache.commons.lang3.StringUtils;
public class NullPointerExceptionExample {
private static void printLength(String str) {
if (StringUtils.isNotEmpty(str)) {
System.out.println(str.length());
} else {
System.out.println("Empty string");
}
}
public static void main(String args[]) {
String myString = null;
printLength(myString);
}
}
Tips for Preventing NullPointerException
To prevent NullPointerException
, one can take the following measures:
- Verify that an object is initialized properly by checking for null before accessing its methods or properties.
- Utilize Apache Commons
StringUtils
for String operations, such as usingStringUtils.isNotEmpty()
to confirm that a string is not empty before proceeding with it.
- When possible, use primitives instead of objects since they cannot contain null references. For example, consider using
int
instead ofInteger
andboolean
instead ofBoolean
.
Leave a Reply