A NullPointerException (NPE) is an unchecked runtime exception that occurs when Java attempts to use a null reference where an object is required. It belongs to the java.lang package.
- Commonly occurs when accessing instance methods, fields, or arrays through null references.
- Can be managed using null checks, input validation, and suitable default values.
Example: Program to demonstrate a NullPointerException.
class Geeks{
public static void main(String[] args){
String s = null;
// Accessing a method using a null reference
System.out.println(s.length());
}
}
Output:

Explanation: The variable s contains null and does not refer to any String object. Calling length() through this reference causes the JVM to throw a NullPointerException.
Common Causes of NullPointerException
A NullPointerException occurs when an operation requires an object but encounters a null reference.
- Calling an instance method or accessing a field through a null reference.
- Accessing elements or the length property of a null array.
- Unboxing a null wrapper object into a primitive type.
- Dereferencing a null value returned by a method.
How to Avoid NullPointerException
NullPointerException can be avoided by checking references before using them and validating values at appropriate points in the application.
1. Check for Null Before Using an Object
The simplest way to avoid NullPointerException is to check whether an object reference is null before accessing its methods or fields.
class Geeks{
public static void main(String[] args){
String s = null;
if (s != null){
System.out.println(s.length());
} else {
System.out.println("String is null");
}
}
}
Output
String is null
Explanation: The if condition checks whether s refers to an object. Since it contains null, the program executes the else block instead of calling length().
2. Validate Method Arguments Using Objects.requireNonNull()
The Objects.requireNonNull() method checks whether a reference is null. It throws a NullPointerException with an optional custom message if validation fails.
import java.util.Objects;
class Geeks{
public static void main(String[] args){
String name = null;
Objects.requireNonNull(name, "Name cannot be null");
System.out.println(name.length());
}
}
Output:

Explanation: The requireNonNull() method checks the reference before the length() operation. Since name is null, it immediately throws a NullPointerException with the specified message.
3. Use Default Values When Appropriate
A default value can be assigned when a reference is null and the application has a meaningful alternative.
class Geeks{
public static void main(String[] args){
String name = null;
String result = (name != null) ? name : "Unknown";
System.out.println(result);
}
}
Output
Unknown
Explanation: The ternary operator checks whether name is null. Since it is null, the program assigns "Unknown" to result instead of accessing the original reference.
4. Use Optional for Potentially Absent Values
The Optional class, introduced in Java 8, represents values that may be absent.
import java.util.Optional;
class Geeks{
public static void main(String[] args){
String name = null;
Optional<String> result = Optional.ofNullable(name);
System.out.println(result.orElse("Default Name"));
}
}
Output
Default Name
Explanation: The ofNullable() method creates an empty Optional when the supplied value is null. The orElse() method returns the specified default value when no value is present.
Note: Optional is particularly useful for method return values that may be absent. It does not automatically prevent NullPointerException when the contained value is accessed incorrectly.
Practices to Prevent NullPointerException
- Initialize object references before using them.
- Validate method arguments using Objects.requireNonNull() when null values are not allowed.
- Return Optional when a method may not produce a result.
- Use meaningful default values when an absent value has a valid alternative.
- Avoid unnecessary null assignments and perform null checks before accessing objects.