# java.lang.NegativeArraySizeException: -1

- **ID:** `java/negative-array-size-exception`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

An attempt to create an array with a negative size, typically caused by an integer overflow, a bug in size calculation, or malformed input data.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

1. **Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size);** (95% success)
   ```
   Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size);
   ```
2. **Use Math.max(0, size) to clamp the size to a non-negative value as a temporary fix, but investigate the root cause.** (70% success)
   ```
   Use Math.max(0, size) to clamp the size to a non-negative value as a temporary fix, but investigate the root cause.
   ```

## Dead Ends

- **** — The issue is not about memory limits but a negative value; increasing heap does not fix the logic error. (90% fail)
- **** — This masks the underlying bug, potentially leading to data corruption or silent failures. (80% fail)
