Avoid potential null dereference with NSError

Potential null dereference.  According to coding standards in 'Creating
and Returning NSError Objects' the parameter may be null.

I've added checks for `if (error != NULL)`. I believe `if (error)` would work to, but I feel explicitly checking `NULL`  explains the intention better. If you disagree I will amend the pull request to use `if (error)`.