### Godot version

41e5f5b0930343ef548a56d553ba20fafd672147

### System information

Windows 10, Vulkan, Intel® Core™ i7-8700K, Nvidia GTX 1080 (471.11)

### Issue description

The editor will crash if you select an AnimationTree node then select another node. This seems to be caused by the edit function assigning a null object via the edit interface, then calling another function which does not appear to be null.

### Steps to reproduce

Create an AnimationTree node then select another node.

### Minimal reproduction project

N/A
parents.push_back(prev);
			p_nodesm = Ref<AnimationNodeStateMachine>(prev);
			prev_path += "../";
			prev = prev->get_prev_state_machine();
		}
		end_menu->add_item("Root", nodes_to_connect.size());
		nodes_to_connect.push_back(prev_path + state_machine->end_node);
		prev_path.remove_at(prev_path.size() - 1);
	}
	List<StringName> nodes;
	p_nodesm->get_node_list(&nodes);
	PopupMenu *nodes_menu = memnew(PopupMenu);
	nodes_menu->set_name(p_name);
	nodes_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
	p_menu->add_child(nodes_menu);
	bool node_added = false;
	for (const StringName &E : nodes) {
		if (p_nodesm->can_edit_node(E)) {
			Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E);
			String path;
			if (from_root) {
				path = prev_path + "/" + E;
			} else {
				path = String(p_path) + "/" + E;
			}
			if (ansm == state_machine) {
				end_menu->add_item(E, nodes_to_connect.size());
				nodes_to_connect.push_back(state_machine->end_node);
				continue;
			}