{"record":{"id":"7a50d5d70c6649cf","repo":"PhilJay/MPAndroidChart","slug":"object-pool-must-be-instantiated-with-a-capacity-g","errorCode":null,"errorMessage":"Object Pool must be instantiated with a capacity greater than 0!","messagePattern":"Object Pool must be instantiated with a capacity greater than 0!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java","lineNumber":56,"sourceCode":"\n    /**\n     * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object.\n     *\n     * @param withCapacity A positive integer value.\n     * @param object An instance of the object that the pool should recycle.\n     * @return\n     */\n    public static synchronized ObjectPool create(int withCapacity, Poolable object){\n        ObjectPool result = new ObjectPool(withCapacity, object);\n        result.poolId = ids;\n        ids++;\n\n        return result;\n    }\n\n    private ObjectPool(int withCapacity, T object){\n        if(withCapacity <= 0){\n            throw new IllegalArgumentException(\"Object Pool must be instantiated with a capacity greater than 0!\");\n        }\n        this.desiredCapacity = withCapacity;\n        this.objects = new Object[this.desiredCapacity];\n        this.objectsPointer = 0;\n        this.modelObject = object;\n        this.replenishPercentage = 1.0f;\n        this.refillPool();\n    }\n\n    /**\n     * Set the percentage of the pool to replenish on empty.  Valid values are between\n     * 0.00f and 1.00f\n     *\n     * @param percentage a value between 0 and 1, representing the percentage of the pool to replenish.\n     */\n    public void setReplenishPercentage(float percentage){\n        float p = percentage;\n        if(p > 1){","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/PhilJay/MPAndroidChart/blob/9c7275a0596a7ac0e50ca566e680f7f9d73607af/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a positive integer capacity: ObjectPool.create(Math.max(1, capacity), sample).","Validate the source config before pool creation and default to a sane minimum (e.g. 16).","Clamp computed capacities: int cap = desired > 0 ? desired : DEFAULT_CAPACITY;.","Add a unit test asserting create() rejects non-positive inputs so regressions surface early."],"exampleFix":"// before\nint cap = config.getMaxPoolSize(); // can be 0\nObjectPool pool = ObjectPool.create(cap, samplePoolable); // throws\n\n// after\nint cap = Math.max(1, config.getMaxPoolSize());\nObjectPool pool = ObjectPool.create(cap, samplePoolable);","handlingStrategy":"validation","validationCode":"int cap = withCapacity > 0 ? withCapacity : DEFAULT_CAPACITY; // e.g. 16\nObjectPool pool = ObjectPool.create(cap, samplePoolable);","typeGuard":"boolean isValidCapacity(int cap) {\n    return cap > 0;\n}","tryCatchPattern":null,"preventionTips":["Clamp externally-sourced capacities to a minimum of 1.","Default to a sensible pool size when config is absent.","Unit-test pool creation with 0 and negative values."],"tags":["object-pool","capacity","constructor","validation"],"backgroundTag":null,"analyzedSha":"9c7275a0596a7ac0e50ca566e680f7f9d73607af","analyzedAt":"2026-08-14T00:17:56.136Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}