JessYanCoding/AndroidAutoSize · error · java.lang.IllegalArgumentException

negative size:

Error message

negative size: 

What it means

badElementIndex() is the private formatter behind checkElementIndex(). If the size argument passed alongside an index is negative, it throws IllegalArgumentException("negative size: N") because an element count can never be negative. This indicates a caller bug: a computed count (e.g. list.size() from a mangled data structure or an off-by-one subtraction) produced a negative value.

Solutions

  1. Fix the upstream computation so the size is a real, non-negative count.
  2. Clamp or reject negative values at the source: if (size < 0) throw ... or Math.max(0, size) where 0 is a valid empty case.
  3. Replace sentinel -1 usages (e.g. indexOf results) with explicit not-found handling before deriving a size.
  4. Log the index/size arguments to identify which call site passes the negative size.

Example fix

// before
int size = end - start; // can be negative
Preconditions.checkElementIndex(index, size);

// after
int size = Math.max(0, end - start);
Preconditions.checkElementIndex(index, size);
Defensive patterns

Strategy: validation

Validate before calling

public static void safeCheckElementIndex(int index, int size) {
    if (size < 0) throw new IllegalArgumentException("size must be >= 0, got " + size);
    Preconditions.checkElementIndex(index, size);
}

Type guard

public static boolean isValidSize(int size) {
    return size >= 0;
}

Try / catch

try {
    Preconditions.checkElementIndex(index, size);
} catch (IllegalArgumentException e) {
    Log.e("Bounds", "bad index/size: " + index + "/" + size, e);
    throw new IllegalStateException("caller bug: negative or out-of-range size", e);
}

Prevention

When it happens

Trigger: Calling checkElementIndex(index, size) with size < 0 — typically size computed as (end - start) or (count - n) where the subtraction underflowed, or a field initialized to -1 used as a size.

Common situations: Off-by-one arithmetic on collection bounds, sentinel value -1 leaking into a size parameter, or parsing/substring logic where a found-index of -1 is reused as a length.

Related errors


AI-assisted analysis of JessYanCoding/AndroidAutoSize@e402ecdd99 (2026-09-07). Data as JSON: /api/errors/80d9a9ba03e8a121. Report an issue: GitHub.

Appendix: source

Thrown at autosize/src/main/java/me/jessyan/autosize/utils/Preconditions.java:121

    }

    /**
     * Throws {@link IllegalStateException} if the calling thread is not the application's main
     * thread.
     *
     * @throws IllegalStateException If the calling thread is not the application's main thread.
     */
    public static void checkMainThread() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new IllegalStateException("Not in applications main thread");
        }
    }

    private static String badElementIndex(int index, int size, String desc) {
        if (index < 0) {
            return format("%s (%s) must not be negative", new Object[]{desc, Integer.valueOf(index)});
        } else if (size < 0) {
            throw new IllegalArgumentException((new StringBuilder(26)).append("negative size: ").append(size).toString());
        } else {
            return format("%s (%s) must be less than size (%s)", new Object[]{desc, Integer.valueOf(index), Integer.valueOf(size)});
        }
    }

    public static int checkPositionIndex(int index, int size) {
        return checkPositionIndex(index, size, "index");
    }

    public static int checkPositionIndex(int index, int size, String desc) {
        if (index >= 0 && index <= size) {
            return index;
        } else {
            throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
        }
    }

    private static String badPositionIndex(int index, int size, String desc) {

View on GitHub (pinned to e402ecdd99)