fix segmentation fault by index error