Add check to prevent null array errors