i=types.length;

Why don't you try changing the place of this line of code a little down?
Try to understand the exception: ArrayIndexOutOfBounds