The original code was extracted from the ElementHierarchyPicker dialog code behind, which didn't check for null.