C# 使用Unity时出现NullReferenceException问题

Unity报错:NullReferenceException: Object reference not set to an instance of an object
Farm.Inventory.showItemToolTip.OnPointerEnter (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/script/inventory/UI/showItemToolTip.cs:17)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerEnterHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:29)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

NullReferenceException: Object reference not set to an instance of an object
Farm.Inventory.showItemToolTip.OnPointerExit (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/script/inventory/UI/showItemToolTip.cs:26)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerExitHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:36)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272)
UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

using UnityEngine;
using UnityEngine.EventSystems;
namespace Farm.Inventory{
    [RequireComponent(typeof(slotUI))]
    public class showItemToolTip : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
    {
        private slotUI slotUI;
        private inventoryUI inventoryUI=>GetComponentInParent<inventoryUI>();
        private void Awake() {
            slotUI=GetComponent<slotUI>();
        }
        public void OnPointerEnter(PointerEventData eventData)
        {
            if(slotUI.itemAmount!=0) {
                inventoryUI.itemtooltip.gameObject.SetActive(true);
                inventoryUI.itemtooltip.SetupToolTip(slotUI.itemDetails,slotUI.slotType);
            } else {
                inventoryUI.itemtooltip.gameObject.SetActive(false);
            }
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            inventoryUI.itemtooltip.gameObject.SetActive(false);
        }
    }
}



if(slotUI.itemAmount!=0) {
                inventoryUI.itemtooltip.gameObject.SetActive(true);
                inventoryUI.itemtooltip.SetupToolTip(slotUI.itemDetails,slotUI.slotType);
 } 
else {
                inventoryUI.itemtooltip.gameObject.SetActive(false);
 }

是在这一段中 inventoryUI 或者 inventoryUI.itemtooltip 为null
大概率是

inventoryUI=>GetComponentInParent<inventoryUI>();//这里获取不成功

可以用print(inventoryUI) 检查一下是否输出为 Null

是你这个初始化没有获取到

private inventoryUI inventoryUI=>GetComponentInParent<inventoryUI>();

img