Ad Space — Top Banner

NegativeArraySizeException

Java Programming Language

Severity: Moderate

What Does This Error Mean?

NegativeArraySizeException is thrown when you try to create an array with a negative size. For example: int[] numbers = new int[-5]; — you cannot have an array of negative 5 items, so Java crashes. This usually happens because a variable that should be a positive number ended up negative — often due to a calculation error or unexpected input. The fix is to make sure the size value is always zero or positive before creating the array.

Affected Models

  • Java 1.0 and later
  • Java SE
  • Java EE
  • Android

Common Causes

  • Creating an array using a variable that turned out to be negative due to a calculation mistake
  • Subtracting two values to get the array size without checking that the result is positive
  • Reading an array size from user input or a file without validating it is a positive number
  • An off-by-one error in size calculation produces -1 instead of 0
  • Integer overflow in a size calculation wraps around to a very large negative number

How to Fix It

  1. Find the line in the stack trace where the array is created. Look at the variable or expression used for the size.

    Add a System.out.println(size) just before the array creation to see what value it actually has at runtime.

  2. Add a guard before creating the array: if (size < 0) throw new IllegalArgumentException('Size must not be negative: ' + size);

    This gives a more meaningful error message than NegativeArraySizeException and points to the source of the bad value.

  3. Trace back through the code to find where the negative value is being set. Common causes: subtraction that goes below zero, a variable not initialized, or reading from user input without validation.

    Use a debugger or add print statements to track the variable's value through the calculation.

  4. If the size comes from user input or an external source, validate it immediately: int size = Integer.parseInt(input); if (size < 0) { show error to user and return; }

    Never trust external input to be a valid positive integer — always validate at the point of entry.

  5. If you are computing a size from a difference (like endIndex - startIndex), use Math.max(0, endIndex - startIndex) to ensure the result is never negative.

    This is a defensive approach — it means an empty array (size 0) is created instead of crashing, which is often the right behavior for edge cases.

When to Call a Professional

NegativeArraySizeException is always fixable yourself. Add a validation check before creating the array to ensure the size is zero or greater. Then trace back through the code to find where the negative value is coming from.

Frequently Asked Questions

Can an array have a size of zero in Java?

Yes — a zero-length array is perfectly valid in Java. int[] empty = new int[0]; creates an empty array with no elements. This is different from null — the array exists, it just has nothing in it. A zero-length array is useful as a safe return value from methods that might return 'nothing'.

How is NegativeArraySizeException different from ArrayIndexOutOfBoundsException?

NegativeArraySizeException happens when you CREATE an array with a negative size. ArrayIndexOutOfBoundsException happens when you try to ACCESS an element at an index that does not exist. They are both array-related errors but happen at different points — creation vs access.

What is integer overflow and how can it cause this error?

Integer overflow happens when a calculation produces a number larger than int can hold (2,147,483,647). When int overflows, it wraps around to a very large negative number. If that overflowed value is then used as an array size, you get NegativeArraySizeException. If your calculations might produce very large numbers, consider using long instead of int.