java.lang.NegativeArraySizeException: -1
An array was created with a negative length.
Seen on:
Java
Meaning
Usually the length comes from a calculation (subtraction, indexOf result -1, or an overflowed int) that ended below zero.
Common causes
- Size computed as
a - bwith b greater indexOfreturning -1 used as a size- Integer overflow producing a negative number
- Corrupt or unexpected input length field
⚡ Quick fix
- Validate the size before allocating
- Check
indexOfresults - Use
Math.max(0, n)orMath.addExactto catch overflow
Detailed fix by platform
Java
if (n < 0) throw new IllegalArgumentException("size=" + n);
How to diagnose
- Value — Printed size?
- Source — Computation or input?
- Overflow — Large ints?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Most viewed in Java
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Did this fix work for you?
Report a correction or suggest an improvement
Last updated 2 Dec 2026