JAVAWarningRuntime ErrorApril 27, 2026

Runtime Error

java.lang.IllegalArgumentException: Bad char in name at java.base/java.lang.StringCoding$StringDecoder.decode(StringCoding.java:223)

What This Error Means

This error occurs when a program attempts to decode a string using a charset that contains invalid or non-ASCII characters.

Why It Happens

This error happens when a method like `URI.decode()` or `URLDecoder.decode()` encounters a string that contains non-ASCII characters or invalid characters in the charset specified. For example, if you try to decode a URL that contains a non-ASCII character using an incorrect charset.

How to Fix It

  1. 1To fix this error, you need to ensure that you are using the correct charset when decoding strings. You can do this by specifying the correct charset when calling methods like `URI.decode()` or `URLDecoder.decode()`. If you are encoding and decoding strings between different platforms or systems, be aware that different platforms may use different default charsets.

Example Code Solution

❌ Before (problematic code)
Java
String encodedUrl = java.net.URLEncoder.encode("http://example.com/");
String decodedUrl = new java.net.URL(encodedUrl).getPath();
✅ After (fixed code)
Java
String encodedUrl = java.net.URLEncoder.encode("http://example.com/");
String charset = "UTF-8";
String decodedUrl = java.net.URLDecoder.decode(encodedUrl, charset);

Fix for java.lang.IllegalArgumentException: Bad char in name at java.base/java.lang.StringCoding$StringDecoder.decode(StringCoding.java:223)

Related JAVA Errors

Related JAVA Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error