NullReferenceException (782900)

I am working on a 3D game and in the scripting part, I am having a NullReference Exception.

Someone please please help me, stuck for a long time.

NullReferenceException: Object reference not set to an instance of an object
Photo.OnDrop () (at Assets/Scripts/Photo.cs:44)
Inventory.RemoveItem (IInventoryItem item) (at Assets/Scripts/Inventory.cs:44)
ItemDropHandler.OnDrop (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/Scripts/ItemDropHandler.cs:27)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IDropHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:85)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Photo : InventoryItemBase
{
    public GameObject replace;

    public Inventory inventory;

    public HUD Hud;
    // Start is called before the first frame update
    public override string Name
    {
        get
        {
            return "Photo";
        }
    }

    public override void OnDrop()
    {
        RaycastHit hit = new RaycastHit();
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        replace = GameObject.FindGameObjectWithTag("replace");
        if (Physics.Raycast(ray, out hit, 1000))
        {
            //TODO item drop functionality on a fixed wall
            gameObject.SetActive(true);
            //gameObject.transform.position = hit.point;
            //Destroy(replace.gameObject);
            if (hit.collider.gameObject.CompareTag("replace"))
            {
               // gameObject.SetActive(true);
                gameObject.transform.position = replace.gameObject.transform.position;
                gameObject.transform.rotation = replace.gameObject.transform.rotation;
                Destroy(replace.gameObject);
              
            }
            else
            {
                gameObject.SetActive(false);
                IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();
                inventory.AddItem(_item);
                _item.OnPickup();
                Hud.CloseMessagePanel();
            }
         
        }
    }
}

You should put the code up you’re having trouble with :slight_smile:

I am having trouble with this code at line number 44

inventory.AddItem(_item);

I’m guessing _item doesn’t exist when you’re trying to add it because:

IInventoryItem _item = gameObject.GetComponent<IInventoryItem>();

can’t GetComponent because it’s not there. Null Reference always means GetComponent can’t find the thing it’s meant to be getting and returning this error. IInventoryItem isn’t declred in this class so I don’t know what that’s doing.
If you make and IInventoryItem public at the start of your code, you will be able to see in the inspector which is the problem.