prevent add observer for nil object.

If add observer for nil object, the observer can't be removed, which could make the observer hanged. The patch prevent this kind of situation.