java
runtime_error
ai_generated
true
java.lang.NegativeArraySizeException: -1
ID: java/negative-array-size-exception
90%Fix Rate
82%Confidence
1Evidence
2023-08-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
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.
generic中文
尝试创建大小为负数的数组,通常由整数溢出、大小计算错误或格式错误的输入数据引起。
Official Documentation
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/NegativeArraySizeException.htmlWorkarounds
-
95% success Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size);
Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size); -
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.
Use Math.max(0, size) to clamp the size to a non-negative value as a temporary fix, but investigate the root cause.
中文步骤
Validate the array size before creation: if (size < 0) throw new IllegalArgumentException("Size must be non-negative: " + size);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
Common approaches that don't work:
-
90% fail
The issue is not about memory limits but a negative value; increasing heap does not fix the logic error.
-
80% fail
This masks the underlying bug, potentially leading to data corruption or silent failures.