PhilJay/MPAndroidChart · error · IllegalArgumentException

Object Pool must be instantiated with a capacity greater tha

Error message

Object Pool must be instantiated with a capacity greater than 0!

What it means

Thrown by the private ObjectPool constructor when withCapacity is <= 0. ObjectPool.create(int, Poolable) forwards directly, so passing zero or a negative capacity is rejected with IllegalArgumentException. The pool pre-allocates an Object[capacity] array and refills it, which is meaningless at capacity 0.

Source

Thrown at MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java:56

    /**
     * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object.
     *
     * @param withCapacity A positive integer value.
     * @param object An instance of the object that the pool should recycle.
     * @return
     */
    public static synchronized ObjectPool create(int withCapacity, Poolable object){
        ObjectPool result = new ObjectPool(withCapacity, object);
        result.poolId = ids;
        ids++;

        return result;
    }

    private ObjectPool(int withCapacity, T object){
        if(withCapacity <= 0){
            throw new IllegalArgumentException("Object Pool must be instantiated with a capacity greater than 0!");
        }
        this.desiredCapacity = withCapacity;
        this.objects = new Object[this.desiredCapacity];
        this.objectsPointer = 0;
        this.modelObject = object;
        this.replenishPercentage = 1.0f;
        this.refillPool();
    }

    /**
     * Set the percentage of the pool to replenish on empty.  Valid values are between
     * 0.00f and 1.00f
     *
     * @param percentage a value between 0 and 1, representing the percentage of the pool to replenish.
     */
    public void setReplenishPercentage(float percentage){
        float p = percentage;
        if(p > 1){

View on GitHub (pinned to 9c7275a059)

Solutions

  1. Pass a positive integer capacity: ObjectPool.create(Math.max(1, capacity), sample).
  2. Validate the source config before pool creation and default to a sane minimum (e.g. 16).
  3. Clamp computed capacities: int cap = desired > 0 ? desired : DEFAULT_CAPACITY;.
  4. Add a unit test asserting create() rejects non-positive inputs so regressions surface early.

Example fix

// before
int cap = config.getMaxPoolSize(); // can be 0
ObjectPool pool = ObjectPool.create(cap, samplePoolable); // throws

// after
int cap = Math.max(1, config.getMaxPoolSize());
ObjectPool pool = ObjectPool.create(cap, samplePoolable);
Defensive patterns

Strategy: validation

Validate before calling

int cap = withCapacity > 0 ? withCapacity : DEFAULT_CAPACITY; // e.g. 16
ObjectPool pool = ObjectPool.create(cap, samplePoolable);

Type guard

boolean isValidCapacity(int cap) {
    return cap > 0;
}

Prevention

When it happens

Trigger: Calling ObjectPool.create(0, sample) or ObjectPool.create(-1, sample). Passing a computed capacity (e.g. from a config value, list size, or multiplication) that evaluates to zero or negative.

Common situations: Capacity derived from a user setting or server config that can be 0; initializing the pool from an empty collection's size; off-by-one in sizing math; feature-flag that disables pooling by setting capacity to 0.

Related errors


AI-assisted analysis of PhilJay/MPAndroidChart@9c7275a059 (2026-08-14). Data as JSON: /api/errors/7a50d5d70c6649cf. Report an issue: GitHub.