Fix a null pointer exception in castArray