check write object to be non null before calling close on it