Check for nil before adding object to array

If a nil object is added to an array, it causes an exception and the app will crash. Adding the object only based on a simple check prevents such errors and results in safer code.