NullReferenceException: Object reference not set to an instance of an object

Full error:
NullReferenceException: Object reference not set to an instance of an object
LoadGameData.LoadData () (at Assets/Skrypt/LoadGameData.cs:55)
LoadGameData.Start () (at Assets/Skrypt/LoadGameData.cs:19)

code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using UnityEngine.UI;

public class LoadGameData : MonoBehaviour
{


    public TextAsset GameData;
    public GameObject ShopPrefab;
    public GameObject StorePanel;

    public void Start()
    {


        LoadData();
    }

    public void LoadData()
    {

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.LoadXml(GameData.text);

        XmlNodeList StoreList = xmlDoc.GetElementsByTagName("store");

        foreach (XmlNode StoreInfo in StoreList)
        {

            GameObject NewStore = (GameObject)Instantiate(ShopPrefab);

            store storeobj = NewStore.GetComponent<store>();



            XmlNodeList StoreNodes = StoreInfo.ChildNodes;
            foreach (XmlNode StoreNode in StoreNodes)
            {
                if (StoreNode.Name == "name")
                {
                   
                    Text StoreText = NewStore.transform.Find("StoreNameText").GetComponent<Text>();
                    StoreText.text = StoreNode.InnerText;
                }


                if (StoreNode.Name == "image")
                {
                    Sprite newSprite = Resources.Load<Sprite>(StoreNode.InnerText);
                    Image StoreImage = NewStore.transform.Find("ImageButtonClick").GetComponent<Image>();
                    StoreImage.sprite = newSprite;


                }

                if (StoreNode.Name == "BaseStoreProfit")
                    storeobj.BaseStoreProfit = float.Parse(StoreNode.InnerText);
                if (StoreNode.Name == "BaseStoreCost")
                    storeobj.BaseStoreCost = float.Parse(StoreNode.InnerText);
                if (StoreNode.Name == "StoreTimer")
                    storeobj.StoreTimer = float.Parse(StoreNode.InnerText);
            }
            NewStore.transform.SetParent(StorePanel.transform);
        }

    }
}

You are trying to access something that does not exist. If the error line you posted is correct, line 55 tries to access an un-initialized (therefore null) variable: StoreImage.

Since you are initializing this in the line before, it is very likely that the call to GetComponent failed and returned null - you should check against that.

The answer is always the same… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

In a nutshell it means… You are trying to access an object without instantiating it… You might need to use the “new” keyword to instantiate it first i.e create an instance of it from class.

public class MyClass
{
   public int Id {get; set;}
}

MyClass myClass;

myClass.Id = 0; //error comes here

You will have to use:

myClass = new MyClass();
myClass.Id = 0;

Don’t pointlessly necro a 5 month old thread, it’s against the Community Code of Conduct , the solution was already provided, and the one you provided wouldn’t have even solved their specific issue.