Avoid NPE by checking whether the given element is null before doing
anything else with it