Check if object is undefined before checking property of it.

From #6853.