Why is this function called with a nil error?