Home › Java

java.lang.IllegalArgumentException — how to fix

Exception in thread "main" java.lang.IllegalArgumentException: No enum constant com.example.Color.red
java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
java.lang.IllegalArgumentException: Illegal character in path at index 27

Quick fix: Read the message and the first stack frame from your code. Common fixes: Enum.valueOf needs the exact constant name (use Color.valueOf(s.toUpperCase())); for Spring's '-parameters' error add names (@PathVariable("id")) or compile with -parameters; URL-encode spaces in URIs.

What it means

IllegalArgumentException is an unchecked exception that methods throw when a parameter is invalid: out of range, wrong format, unknown name. It is generic, so the message is what tells you the actual problem. (NumberFormatException is a subclass — see NumberFormatException.)

Look at the stack trace: the top frames are inside the library that rejected the value; go down to the first line in your own package — that's the call to fix.

Cause 1: "No enum constant ..."

Enum.valueOf (and MyEnum.valueOf) requires the exact, case-sensitive constant name:

enum Color { RED, GREEN }

Color.valueOf("red");    // IllegalArgumentException: No enum constant Color.red

Normalise input and handle unknown values:

static Optional<Color> parse(String s) {
    try {
        return Optional.of(Color.valueOf(s.trim().toUpperCase(Locale.ROOT)));
    } catch (IllegalArgumentException e) {
        return Optional.empty();
    }
}

With Jackson or JPA, the same error appears when stored/JSON values don't match constant names. Map them explicitly (@JsonProperty("red") on the constant, @Enumerated(EnumType.STRING) with matching DB values).

Cause 2: Spring "Name for argument of type ... not specified"

Spring Framework 6.1 (Spring Boot 3.2+) no longer guesses parameter names from debug info. If your build doesn't compile with -parameters, annotations without explicit names fail:

@GetMapping("/users/{id}")
User get(@PathVariable Long id)          // fails without -parameters

Fix either way:

User get(@PathVariable("id") Long id)
@RequestParam("page") int page

or enable -parameters. Maven:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <parameters>true</parameters>
  </configuration>
</plugin>

Gradle:

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs << '-parameters'
}

Projects using spring-boot-starter-parent or the Spring Boot Gradle plugin get -parameters by default; this mostly hits custom parents, or running from an IDE that doesn't delegate to the build tool (rebuild after changing settings).

Cause 3: "Illegal character in path/query at index N"

java.net.URI rejects unencoded spaces, |, {, } and similar characters. Encode the parts you insert:

String q = URLEncoder.encode("hello world", StandardCharsets.UTF_8);
URI uri = URI.create("https://api.example.com/search?q=" + q);

URLEncoder produces form encoding (spaces become +), which is right for query values. Index N in the message points at the offending character. You can check strings with the URL encoder/decoder.

Cause 4: argument checks in your own or library code

Validation helpers throw it on purpose:

Objects.checkIndex(i, list.size());              // IndexOutOfBounds, not IAE
Preconditions.checkArgument(n > 0, "n must be > 0: %s", n);   // Guava: IAE
new ArrayList<>(-1);   // IllegalArgumentException: Illegal Capacity: -1
Thread.sleep(-5);      // IllegalArgumentException: timeout value is negative

Fix the value at its source rather than catching the exception. Log the offending value when you validate so the message is useful.

Cause 5: reflection "argument type mismatch"

Method.invoke throws IllegalArgumentException: argument type mismatch when the arguments' types don't match the method's parameters (e.g. passing Integer for a String, or the wrong number of args). Print method.getParameterTypes() and compare.

Last updated 2026-12-05.