Instead of this, I think you'll want:

    if (arrayIndex >= noOfElement) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY); // means null
    } else {