Check index to prevent 'ArrayIndexOutOfBoundsException'