### 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
_create_submenu(connect_menu, state_machine, connecting_to_node, connecting_to_node, true);
	}
	connect_menu->add_submenu_item(TTR("To") + " Animation", connecting_to_node);
	if (state_machine_menu->get_item_count() > 0 || !end_node.is_valid()) {
		connect_menu->add_submenu_item(TTR("To") + " StateMachine", "state_machines");
		connect_menu->add_child(state_machine_menu);
	}
	if (end_node.is_valid()) {
		connect_menu->add_submenu_item(TTR("To") + " End", "end_nodes");
		connect_menu->add_child(end_menu);
	} else {
		state_machine_menu->add_item(connecting_to_node, nodes_to_connect.size());
	}
	nodes_to_connect.push_back(connecting_to_node);
	if (nodes_to_connect.size() == 1) {
		_add_transition();
		return;
	}
	connect_menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));
	connect_menu->popup();
}
bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path, bool from_root, Vector<Ref<AnimationNodeStateMachine>> p_parents) {
	String prev_path;
	Vector<Ref<AnimationNodeStateMachine>> parents = p_parents;
	if (from_root && p_nodesm->get_prev_state_machine() == nullptr) {
		return false;
	}
	if (from_root) {
		AnimationNodeStateMachine *prev = p_nodesm->get_prev_state_machine();
		while (prev != nullptr) {