Tutorials Logic, IN info@tutorialslogic.com

IllegalArgumentException in Java: Causes and Fixes

Meaning and Common Causes

IllegalArgumentException is thrown when a method receives a value that has the correct type but an invalid meaning. The object exists and the method is available, but the argument breaks the rule expected by that method.

This exception is common in constructors, setters, validation methods, enum conversion, date parsing, range checks, and service-layer code. A good Java page should teach both the cause and the prevention pattern: validate early, give a clear message, and keep the object in a valid state.

When debugging it, do not only ask which line failed. Ask which argument was passed, what rule the method expected, and whether the caller or the method should be responsible for checking that rule.

IllegalArgumentException is different from NullPointerException and ClassCastException. It usually means the caller passed something like a negative age, an empty username, an unsupported enum value, or a number outside the allowed range.

The argument type can still be correct. For example, setAge(int age) receives an int, but -5 is not a valid age for most applications. Java cannot know that business rule automatically, so your code must check it.

  • Use it when the caller gave a value that violates the method contract.
  • Include the invalid value or rule in the message when it is safe to show.
  • Validate constructor arguments before assigning them to fields.
  • Prefer clear guards over letting bad values move deeper into the program.

How to Fix and Prevent It

Read the stack trace from top to bottom and find the first line that belongs to your code. Then inspect the argument passed into that method. The fix may be in the caller, not inside the method that throws the exception.

For reusable classes, keep validation inside constructors and setters. For user input, validate at the boundary, convert the input carefully, and show a user-friendly message instead of exposing the raw exception.

  • Check range rules such as age >= 0, pageSize > 0, and index within bounds.
  • Check string rules such as not blank, valid format, and expected value.
  • Use Objects.requireNonNull for required references and custom checks for business rules.
  • Write tests for normal, boundary, and invalid input.

Interview Notes

In interviews, explain that IllegalArgumentException is unchecked and usually signals wrong API usage by the caller. It is useful when a method contract is broken before the method can do meaningful work.

Also mention that throwing it should not replace all validation. In web apps, input validation should normally produce a controlled response, while IllegalArgumentException is more useful inside domain or utility code.

  • It extends RuntimeException.
  • It represents an invalid value, not a missing method or missing class.
  • It is often thrown deliberately by application code.
  • Good exception messages make debugging much faster.

Choose the Right Exception Type

Choose the exception that identifies the broken contract. IllegalArgumentException means the caller supplied an unacceptable value. IllegalStateException means the receiver cannot perform the operation in its current state, even if the argument is valid.

Use NullPointerException for a required reference that is null and an index-specific exception for an invalid position. A domain API may instead expose a named exception when callers need to distinguish a business rejection from programmer misuse.

  • IllegalArgumentException: valid type, invalid value for this method.
  • IllegalStateException: operation is not valid in the current state of the object.
  • NullPointerException: a required reference argument is null.
  • IndexOutOfBoundsException: an index lies outside the accepted range.

Throw IllegalArgumentException for an Invalid Range

Throw IllegalArgumentException for an Invalid Range
class UserProfile {
    private final int age;

    UserProfile(int age) {
        if (age < 0 || age > 130) {
            throw new IllegalArgumentException("age must be between 0 and 130: " + age);
        }
        this.age = age;
    }
}

Validate Before Calling the Method

Validate Before Calling the Method
public class PageRequestDemo {
    static void loadPage(int pageSize) {
        if (pageSize <= 0) {
            throw new IllegalArgumentException("pageSize must be positive");
        }
        System.out.println("Loading " + pageSize + " records");
    }

    public static void main(String[] args) {
        int pageSize = 25;
        loadPage(pageSize);
    }
}
Before you move on

IllegalArgumentException in Java: Causes and Fixes Mastery Check

5 checks
  • Identify the exact argument that caused the exception.
  • Confirm whether the argument type is correct but the value is invalid.
  • Read the method contract, documentation, or validation rule.
  • Fix the caller if it is sending bad data.
  • Add boundary tests for invalid values.

IllegalArgumentException in Java Questions Learners Ask

Throw it when a method receives an argument that violates its contract, such as a negative value where a positive value is required or an option outside the supported set.

IllegalArgumentException is about invalid input to a method. IllegalStateException is about the object being in an invalid state for the operation (e.g., calling close() on an already-closed resource).

Java API convention generally favors NullPointerException for a required reference that is null; Objects.requireNonNull applies that convention. Use IllegalArgumentException for non-null values that violate another method rule, and document the public contract.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.