IllegalArgumentException in Java — What It Means and How to Fix It

What Java's IllegalArgumentException means and how to fix it.

Published November 2, 2026

IllegalArgumentException is thrown when a method receives an argument that is invalid even though its type is right.

What it means and why it happens

Developers throw it deliberately to reject bad input, or JDK methods do (e.g. negative capacity).

Read the full message: it names the file and line where the problem was detected, which is the best place to start.

What you see

run the code
java.lang.IllegalArgumentException: age must be positive

Practical examples

Code that triggers it

setAge(-5);

The fix

void setAge(int age) {
    if (age < 0) throw new IllegalArgumentException("age must be positive: " + age);
    this.age = age;
}

Validation helpers

Objects.requireNonNull(name);
if (list.isEmpty()) throw new IllegalArgumentException("list required");

Common mistakes

  • Vague messages.
  • Using it for state problems (use IllegalStateException).
  • Not validating at API boundaries.

Key takeaways

  • IllegalArgumentException: IllegalArgumentException is thrown when a method receives an argument that is invalid even though its type is right.
  • Read the message and the line number first.
  • Fix the cause, don't just silence the error.

FAQ

IllegalArgument vs IllegalState?

Argument = bad input; state = object not ready for that call.

How do I find which line caused IllegalArgumentException?

The error message and stack trace show the file and line; start there and work backwards.

More Java articles