{"record":{"id":"088df96e5eb03f54","repo":"TheAlgorithms/Java","slug":"null-items-are-not-allowed","errorCode":null,"errorMessage":"Null items are not allowed","messagePattern":"Null items are not allowed","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java","lineNumber":82,"sourceCode":"            return null;\n        }\n\n        Item item = buffer[getPointer.getAndIncrement()];\n        size.decrementAndGet();\n        return item;\n    }\n\n    /**\n     * Adds an item to the end of the buffer (FIFO).\n     * If the buffer is full, this operation will overwrite the oldest data.\n     *\n     * @param item The item to be added.\n     * @throws IllegalArgumentException if the item is null.\n     * @return {@code true} if the item was successfully added, {@code false} if the buffer was full and the item overwrote existing data.\n     */\n    public boolean put(Item item) {\n        if (item == null) {\n            throw new IllegalArgumentException(\"Null items are not allowed\");\n        }\n\n        boolean wasEmpty = isEmpty();\n        if (isFull()) {\n            getPointer.getAndIncrement(); // Move get pointer to discard oldest item\n        } else {\n            size.incrementAndGet();\n        }\n\n        buffer[putPointer.getAndIncrement()] = item;\n        return wasEmpty;\n    }\n\n    /**\n     * The {@code CircularPointer} class is a helper class used to track the current index (pointer)\n     * in the circular buffer.\n     * The max value represents the capacity of the buffer.\n     * The `CircularPointer` class ensures that the pointer automatically wraps around to 0","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java#L64-L100","documentation":"Thrown by CircularBuffer.put(Item) when the supplied item is null. The buffer treats null as a non-value because null is indistinguishable from an uninitialized slot and would corrupt FIFO semantics (a null returned by get() already signals emptiness). The guard runs before any pointer mutation, so a rejected put leaves the buffer state untouched.","triggerScenarios":"Calling circularBuffer.put(null) directly; passing a value sourced from a Map.get() that may return null; feeding a producer whose next() can yield null into the buffer without filtering.","commonSituations":"Producer/consumer pipelines where the upstream source has optional elements; deserialization that maps missing fields to null; refactoring a buffer from a primitive array to a generic type and forgetting null was newly allowed.","solutions":["Filter or replace nulls at the producer before calling put(): if (item != null) buffer.put(item);","Use Optional or a sentinel value to represent 'no data' instead of null.","If null must be representable, wrap items in a small holder object."],"exampleFix":"// before\nbuffer.put(queue.poll());\n// after\nItem next = queue.poll();\nif (next != null) buffer.put(next);","handlingStrategy":"validation","validationCode":"if (item == null) {\n    // skip, log, or substitute a sentinel\n    return;\n}\nbuffer.put(item);","typeGuard":"static <Item> boolean isAddable(Item item) {\n    return item != null;\n}","tryCatchPattern":null,"preventionTips":["Filter producer output for null before enqueueing into the buffer.","Prefer Optional or a sentinel value over null to represent 'no data'.","Add a unit test that asserts put(null) throws to lock the contract."],"tags":["java","circular-buffer","null-check","argument-validation","datastructures"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}