A non-null variable results me to NullReferenceException [Unity]

I’ve been on Unity for a very long time, yet I’ve come across the most common error.
I made a script to create a ship components store from a list of ScriptableObjects.

Here’s the ScriptableObjects:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ShipComponent : ScriptableObject
{
public int shipID;
public string shipName;
[TextArea] public string shipDescription;
public ShipComponentType ComponentType;
public float Mass;
public float Friction;
public float Thrust;
public float Friction2AngularFrictionCoef;
public float Thrust2AngularThrustCoef;
public bool IsQuantum;
public Sprite previewSprite;
}
</code>
<code>public class ShipComponent : ScriptableObject { public int shipID; public string shipName; [TextArea] public string shipDescription; public ShipComponentType ComponentType; public float Mass; public float Friction; public float Thrust; public float Friction2AngularFrictionCoef; public float Thrust2AngularThrustCoef; public bool IsQuantum; public Sprite previewSprite; } </code>
public class ShipComponent : ScriptableObject
{
    public int shipID;
    public string shipName;
    [TextArea] public string shipDescription;
    public ShipComponentType ComponentType;
    public float Mass;
    public float Friction;
    public float Thrust;
    public float Friction2AngularFrictionCoef;
    public float Thrust2AngularThrustCoef;
    public bool IsQuantum;
    public Sprite previewSprite;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class DatabaseComponents : ScriptableObject
{
public int databaseID;
public string databaseName;
public List<Guild> databaseGuilds;
}
</code>
<code>public class DatabaseComponents : ScriptableObject { public int databaseID; public string databaseName; public List<Guild> databaseGuilds; } </code>
public class DatabaseComponents : ScriptableObject
{
    public int databaseID;
    public string databaseName;

    public List<Guild> databaseGuilds;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Guild : ScriptableObject
{
public int guildID;
public string guildName;
public List<GameObject> guildComponents;
}
</code>
<code>public class Guild : ScriptableObject { public int guildID; public string guildName; public List<GameObject> guildComponents; } </code>
public class Guild : ScriptableObject
{
    public int guildID;
    public string guildName;
    public List<GameObject> guildComponents;
}

Here is the entire code where my problem is :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ShipCreatorDEBUG : MonoBehaviour
{
[Header("Component Shop")]
[SerializeField] private Transform shopPanel;
[SerializeField] private GameObject guildNamePrefab;
[SerializeField] private GameObject rowPrefab;
[SerializeField]
private DatabaseComponents database;
private Dictionary<Guild, List<ShipComponent>> guildToListComponents;
private void Start()
{
guildToListComponents = GenerateShipComponentDictionary(database);
GenerateShop();
}
// Shop
void GenerateShop()
{
foreach (Transform trans in shopPanel)
{
Destroy(trans.gameObject);
}
foreach (var entry in guildToListComponents)
{
Guild guild = entry.Key;
List<ShipComponent> shipComponents = entry.Value;
if (shipComponents == null || shipComponents.Count == 0) continue;
GameObject rowName = Instantiate(guildNamePrefab, shopPanel);
TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>();
tmp.text = guild.guildName;
int count = shipComponents.Count;
int rowAmount = Mathf.CeilToInt(count / 3f);
for (int i = 0; i < rowAmount; i++)
{
GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel);
for (int j = 0; j < 3; j++)
{
int currentIndex = i * 3 + j;
if (currentIndex >= count) break;
Transform buttonTransform = rowComponents.transform.GetChild(j);
Button btn = buttonTransform.GetComponent<Button>();
ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here
btn.onClick.AddListener(() => SelectComponentInfo(sc));
Image image = buttonTransform.GetChild(0).GetComponent<Image>();
image.sprite = sc.previewSprite;
}
}
}
}
public void SelectComponentInfo(ShipComponent shipComponent)
{
// TODO
}
public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database)
{
Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>();
foreach (Guild guild in database.databaseGuilds)
{
List<ShipComponent> shipComponentsList = new List<ShipComponent>();
foreach (GameObject componentPrefab in guild.guildComponents)
{
ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>();
if (shipComponentObject != null)
{
shipComponentsList.Add(shipComponentObject.shipComponent);
}
}
guildComponentsDictionary[guild] = shipComponentsList;
}
return guildComponentsDictionary;
}
}
</code>
<code>using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class ShipCreatorDEBUG : MonoBehaviour { [Header("Component Shop")] [SerializeField] private Transform shopPanel; [SerializeField] private GameObject guildNamePrefab; [SerializeField] private GameObject rowPrefab; [SerializeField] private DatabaseComponents database; private Dictionary<Guild, List<ShipComponent>> guildToListComponents; private void Start() { guildToListComponents = GenerateShipComponentDictionary(database); GenerateShop(); } // Shop void GenerateShop() { foreach (Transform trans in shopPanel) { Destroy(trans.gameObject); } foreach (var entry in guildToListComponents) { Guild guild = entry.Key; List<ShipComponent> shipComponents = entry.Value; if (shipComponents == null || shipComponents.Count == 0) continue; GameObject rowName = Instantiate(guildNamePrefab, shopPanel); TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>(); tmp.text = guild.guildName; int count = shipComponents.Count; int rowAmount = Mathf.CeilToInt(count / 3f); for (int i = 0; i < rowAmount; i++) { GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel); for (int j = 0; j < 3; j++) { int currentIndex = i * 3 + j; if (currentIndex >= count) break; Transform buttonTransform = rowComponents.transform.GetChild(j); Button btn = buttonTransform.GetComponent<Button>(); ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here btn.onClick.AddListener(() => SelectComponentInfo(sc)); Image image = buttonTransform.GetChild(0).GetComponent<Image>(); image.sprite = sc.previewSprite; } } } } public void SelectComponentInfo(ShipComponent shipComponent) { // TODO } public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database) { Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>(); foreach (Guild guild in database.databaseGuilds) { List<ShipComponent> shipComponentsList = new List<ShipComponent>(); foreach (GameObject componentPrefab in guild.guildComponents) { ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>(); if (shipComponentObject != null) { shipComponentsList.Add(shipComponentObject.shipComponent); } } guildComponentsDictionary[guild] = shipComponentsList; } return guildComponentsDictionary; } } </code>
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ShipCreatorDEBUG : MonoBehaviour
{
    [Header("Component Shop")]
    [SerializeField] private Transform shopPanel;
    [SerializeField] private GameObject guildNamePrefab;
    [SerializeField] private GameObject rowPrefab;
    [SerializeField]
    private DatabaseComponents database;

    private Dictionary<Guild, List<ShipComponent>> guildToListComponents;

    private void Start()
    {
        guildToListComponents = GenerateShipComponentDictionary(database);
        GenerateShop();
    }

    // Shop
    void GenerateShop()
    {
        foreach (Transform trans in shopPanel)
        {
            Destroy(trans.gameObject);
        }

        foreach (var entry in guildToListComponents)
        {
            Guild guild = entry.Key;
            List<ShipComponent> shipComponents = entry.Value;

            if (shipComponents == null || shipComponents.Count == 0) continue;

            GameObject rowName = Instantiate(guildNamePrefab, shopPanel);
            TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>();
            tmp.text = guild.guildName;

            int count = shipComponents.Count;
            int rowAmount = Mathf.CeilToInt(count / 3f);

            for (int i = 0; i < rowAmount; i++)
            {
                GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel);

                for (int j = 0; j < 3; j++)
                {
                    int currentIndex = i * 3 + j;
                    if (currentIndex >= count) break;

                    Transform buttonTransform = rowComponents.transform.GetChild(j);
                    Button btn = buttonTransform.GetComponent<Button>();

                    ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here

                    btn.onClick.AddListener(() => SelectComponentInfo(sc));

                    Image image = buttonTransform.GetChild(0).GetComponent<Image>();
                    image.sprite = sc.previewSprite;
                }
            }
        }
    }

    public void SelectComponentInfo(ShipComponent shipComponent)
    {
        // TODO
    }

    public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database)
    {
        Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>();

        foreach (Guild guild in database.databaseGuilds)
        {
            List<ShipComponent> shipComponentsList = new List<ShipComponent>();

            foreach (GameObject componentPrefab in guild.guildComponents)
            {
                ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>();

                if (shipComponentObject != null)
                {
                    shipComponentsList.Add(shipComponentObject.shipComponent);
                }
            }

            guildComponentsDictionary[guild] = shipComponentsList;
        }

        return guildComponentsDictionary;
    }
}

Here are the log results :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NullReferenceException: Object reference not set to an instance of an object
ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57)
ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20)
</code>
<code>NullReferenceException: Object reference not set to an instance of an object ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57) ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20) </code>
NullReferenceException: Object reference not set to an instance of an object
ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57)
ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20)

The only thing can be null is the shipComponents list, but it has 2 elements on it. It might be useless to send it because of this line :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (shipComponents == null || shipComponents.Count == 0) continue;
</code>
<code>if (shipComponents == null || shipComponents.Count == 0) continue; </code>
if (shipComponents == null || shipComponents.Count == 0) continue;

Unless the problem is present but I don’t see it, as you can see, the values are non-null but I still get a NullReferenceException error.

Any idea where this problem might be coming from? The function is executed in runtime.

(reasking the question because it seems they don’t understand this is NOT a duplicate of the common NullReferenceException question)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

A non-null variable results me to NullReferenceException [Unity]

I’ve been on Unity for a very long time, yet I’ve come across the most common error.
I made a script to create a ship components store from a list of ScriptableObjects.

Here’s the ScriptableObjects:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ShipComponent : ScriptableObject
{
public int shipID;
public string shipName;
[TextArea] public string shipDescription;
public ShipComponentType ComponentType;
public float Mass;
public float Friction;
public float Thrust;
public float Friction2AngularFrictionCoef;
public float Thrust2AngularThrustCoef;
public bool IsQuantum;
public Sprite previewSprite;
}
</code>
<code>public class ShipComponent : ScriptableObject { public int shipID; public string shipName; [TextArea] public string shipDescription; public ShipComponentType ComponentType; public float Mass; public float Friction; public float Thrust; public float Friction2AngularFrictionCoef; public float Thrust2AngularThrustCoef; public bool IsQuantum; public Sprite previewSprite; } </code>
public class ShipComponent : ScriptableObject
{
    public int shipID;
    public string shipName;
    [TextArea] public string shipDescription;
    public ShipComponentType ComponentType;
    public float Mass;
    public float Friction;
    public float Thrust;
    public float Friction2AngularFrictionCoef;
    public float Thrust2AngularThrustCoef;
    public bool IsQuantum;
    public Sprite previewSprite;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class DatabaseComponents : ScriptableObject
{
public int databaseID;
public string databaseName;
public List<Guild> databaseGuilds;
}
</code>
<code>public class DatabaseComponents : ScriptableObject { public int databaseID; public string databaseName; public List<Guild> databaseGuilds; } </code>
public class DatabaseComponents : ScriptableObject
{
    public int databaseID;
    public string databaseName;

    public List<Guild> databaseGuilds;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Guild : ScriptableObject
{
public int guildID;
public string guildName;
public List<GameObject> guildComponents;
}
</code>
<code>public class Guild : ScriptableObject { public int guildID; public string guildName; public List<GameObject> guildComponents; } </code>
public class Guild : ScriptableObject
{
    public int guildID;
    public string guildName;
    public List<GameObject> guildComponents;
}

Here is the entire code where my problem is :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ShipCreatorDEBUG : MonoBehaviour
{
[Header("Component Shop")]
[SerializeField] private Transform shopPanel;
[SerializeField] private GameObject guildNamePrefab;
[SerializeField] private GameObject rowPrefab;
[SerializeField]
private DatabaseComponents database;
private Dictionary<Guild, List<ShipComponent>> guildToListComponents;
private void Start()
{
guildToListComponents = GenerateShipComponentDictionary(database);
GenerateShop();
}
// Shop
void GenerateShop()
{
foreach (Transform trans in shopPanel)
{
Destroy(trans.gameObject);
}
foreach (var entry in guildToListComponents)
{
Guild guild = entry.Key;
List<ShipComponent> shipComponents = entry.Value;
if (shipComponents == null || shipComponents.Count == 0) continue;
GameObject rowName = Instantiate(guildNamePrefab, shopPanel);
TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>();
tmp.text = guild.guildName;
int count = shipComponents.Count;
int rowAmount = Mathf.CeilToInt(count / 3f);
for (int i = 0; i < rowAmount; i++)
{
GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel);
for (int j = 0; j < 3; j++)
{
int currentIndex = i * 3 + j;
if (currentIndex >= count) break;
Transform buttonTransform = rowComponents.transform.GetChild(j);
Button btn = buttonTransform.GetComponent<Button>();
ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here
btn.onClick.AddListener(() => SelectComponentInfo(sc));
Image image = buttonTransform.GetChild(0).GetComponent<Image>();
image.sprite = sc.previewSprite;
}
}
}
}
public void SelectComponentInfo(ShipComponent shipComponent)
{
// TODO
}
public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database)
{
Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>();
foreach (Guild guild in database.databaseGuilds)
{
List<ShipComponent> shipComponentsList = new List<ShipComponent>();
foreach (GameObject componentPrefab in guild.guildComponents)
{
ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>();
if (shipComponentObject != null)
{
shipComponentsList.Add(shipComponentObject.shipComponent);
}
}
guildComponentsDictionary[guild] = shipComponentsList;
}
return guildComponentsDictionary;
}
}
</code>
<code>using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class ShipCreatorDEBUG : MonoBehaviour { [Header("Component Shop")] [SerializeField] private Transform shopPanel; [SerializeField] private GameObject guildNamePrefab; [SerializeField] private GameObject rowPrefab; [SerializeField] private DatabaseComponents database; private Dictionary<Guild, List<ShipComponent>> guildToListComponents; private void Start() { guildToListComponents = GenerateShipComponentDictionary(database); GenerateShop(); } // Shop void GenerateShop() { foreach (Transform trans in shopPanel) { Destroy(trans.gameObject); } foreach (var entry in guildToListComponents) { Guild guild = entry.Key; List<ShipComponent> shipComponents = entry.Value; if (shipComponents == null || shipComponents.Count == 0) continue; GameObject rowName = Instantiate(guildNamePrefab, shopPanel); TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>(); tmp.text = guild.guildName; int count = shipComponents.Count; int rowAmount = Mathf.CeilToInt(count / 3f); for (int i = 0; i < rowAmount; i++) { GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel); for (int j = 0; j < 3; j++) { int currentIndex = i * 3 + j; if (currentIndex >= count) break; Transform buttonTransform = rowComponents.transform.GetChild(j); Button btn = buttonTransform.GetComponent<Button>(); ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here btn.onClick.AddListener(() => SelectComponentInfo(sc)); Image image = buttonTransform.GetChild(0).GetComponent<Image>(); image.sprite = sc.previewSprite; } } } } public void SelectComponentInfo(ShipComponent shipComponent) { // TODO } public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database) { Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>(); foreach (Guild guild in database.databaseGuilds) { List<ShipComponent> shipComponentsList = new List<ShipComponent>(); foreach (GameObject componentPrefab in guild.guildComponents) { ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>(); if (shipComponentObject != null) { shipComponentsList.Add(shipComponentObject.shipComponent); } } guildComponentsDictionary[guild] = shipComponentsList; } return guildComponentsDictionary; } } </code>
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ShipCreatorDEBUG : MonoBehaviour
{
    [Header("Component Shop")]
    [SerializeField] private Transform shopPanel;
    [SerializeField] private GameObject guildNamePrefab;
    [SerializeField] private GameObject rowPrefab;
    [SerializeField]
    private DatabaseComponents database;

    private Dictionary<Guild, List<ShipComponent>> guildToListComponents;

    private void Start()
    {
        guildToListComponents = GenerateShipComponentDictionary(database);
        GenerateShop();
    }

    // Shop
    void GenerateShop()
    {
        foreach (Transform trans in shopPanel)
        {
            Destroy(trans.gameObject);
        }

        foreach (var entry in guildToListComponents)
        {
            Guild guild = entry.Key;
            List<ShipComponent> shipComponents = entry.Value;

            if (shipComponents == null || shipComponents.Count == 0) continue;

            GameObject rowName = Instantiate(guildNamePrefab, shopPanel);
            TMP_Text tmp = rowName.GetComponentInChildren<TMP_Text>();
            tmp.text = guild.guildName;

            int count = shipComponents.Count;
            int rowAmount = Mathf.CeilToInt(count / 3f);

            for (int i = 0; i < rowAmount; i++)
            {
                GameObject rowComponents = Instantiate(guildNamePrefab, shopPanel);

                for (int j = 0; j < 3; j++)
                {
                    int currentIndex = i * 3 + j;
                    if (currentIndex >= count) break;

                    Transform buttonTransform = rowComponents.transform.GetChild(j);
                    Button btn = buttonTransform.GetComponent<Button>();

                    ShipComponent sc = shipComponents[currentIndex]; // NullReferenceException here

                    btn.onClick.AddListener(() => SelectComponentInfo(sc));

                    Image image = buttonTransform.GetChild(0).GetComponent<Image>();
                    image.sprite = sc.previewSprite;
                }
            }
        }
    }

    public void SelectComponentInfo(ShipComponent shipComponent)
    {
        // TODO
    }

    public Dictionary<Guild, List<ShipComponent>> GenerateShipComponentDictionary(DatabaseComponents database)
    {
        Dictionary<Guild, List<ShipComponent>> guildComponentsDictionary = new Dictionary<Guild, List<ShipComponent>>();

        foreach (Guild guild in database.databaseGuilds)
        {
            List<ShipComponent> shipComponentsList = new List<ShipComponent>();

            foreach (GameObject componentPrefab in guild.guildComponents)
            {
                ShipComponentObject shipComponentObject = componentPrefab.GetComponent<ShipComponentObject>();

                if (shipComponentObject != null)
                {
                    shipComponentsList.Add(shipComponentObject.shipComponent);
                }
            }

            guildComponentsDictionary[guild] = shipComponentsList;
        }

        return guildComponentsDictionary;
    }
}

Here are the log results :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NullReferenceException: Object reference not set to an instance of an object
ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57)
ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20)
</code>
<code>NullReferenceException: Object reference not set to an instance of an object ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57) ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20) </code>
NullReferenceException: Object reference not set to an instance of an object
ShipCreatorDEBUG.GenerateShop () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:57)
ShipCreatorDEBUG.Start () (at Assets/Assets/Scripts/TestS/ShipCreatorDEBUG.cs:20)

The only thing can be null is the shipComponents list, but it has 2 elements on it. It might be useless to send it because of this line :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (shipComponents == null || shipComponents.Count == 0) continue;
</code>
<code>if (shipComponents == null || shipComponents.Count == 0) continue; </code>
if (shipComponents == null || shipComponents.Count == 0) continue;

Unless the problem is present but I don’t see it, as you can see, the values are non-null but I still get a NullReferenceException error.

Any idea where this problem might be coming from? The function is executed in runtime.

(reasking the question because it seems they don’t understand this is NOT a duplicate of the common NullReferenceException question)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật