Edited Silvias's script to user Array list. Now if there is a field with null value it won't break the csv file