made some changes. Null pointer exception in nested location for loop