handling case of ArrayList containing nulls in new optimized code