alibaba/nacos · error · IllegalArgumentException

Cannot convert String [{}] to Integer

Error message

Cannot convert String [{}] to Integer

What it means

Thrown by IntegerConverter.convert() as an IllegalArgumentException when Integer.valueOf(property) fails. An empty or null string returns null (no error). This converter is used internally by NacosClientProperties to coerce string property values to Integer. The message includes the offending value.

Source

Thrown at client-basic/src/main/java/com/alibaba/nacos/client/env/convert/IntegerConverter.java:31

 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.nacos.client.env.convert;

import com.alibaba.nacos.common.utils.StringUtils;

class IntegerConverter extends AbstractPropertyConverter<Integer> {
    
    @Override
    Integer convert(String property) {
        if (StringUtils.isEmpty(property)) {
            return null;
        }
        try {
            return Integer.valueOf(property);
        } catch (Exception e) {
            throw new IllegalArgumentException(
                "Cannot convert String [" + property + "] to Integer");
        }
    }
}

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Check the property value in the exception message and ensure it is a valid 32-bit signed integer.
  2. Remove thousands separators, units, and whitespace from the value.
  3. If the value exceeds Integer range, consider whether a Long property is more appropriate.

Example fix

// before
props.setProperty("some.int.property", "1,000");

// after
props.setProperty("some.int.property", "1000");
Defensive patterns

Strategy: validation

Validate before calling

String raw = properties.getProperty("some.int.property");
if (raw != null && !raw.isEmpty()) {
    try {
        Integer.parseInt(raw.trim());
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Property must be a valid integer: " + raw, e);
    }
}

Try / catch

try {
    Integer value = Integer.valueOf(raw);
} catch (NumberFormatException e) {
    // Use a safe default or log a warning
    value = defaultValue;
}

Prevention

When it happens

Trigger: A Nacos property expected to be an integer contains a non-numeric string (e.g. 'abc', '1.5', '3,000', '0x1F'). Overflow values exceeding Integer.MAX_VALUE also trigger NumberFormatException internally.

Common situations: Locale-specific number formatting with commas; hexadecimal or scientific notation; copy-paste errors; values with units appended (e.g. '100ms').

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/5ead55ea9f12a891. Report an issue: GitHub.