Search Unity

Feedback NullReferenceException Error

Discussion in 'Getting Started' started by amp3670, Apr 7, 2020.

  1. amp3670

    amp3670

    Joined:
    Apr 1, 2020
    Posts:
    10
    I'm creating a simple child app in Unity. The function that I am making now is a shop function.
    When child buys items at the shop, I want to make sure that child can't buy them again.
    Once an item is purchased at a shop, the item's information is stored in the Inventory.
    IsInven() is to see if there are any items that child wants to purchase in the Inventory already, and if there are items in the Inventory that child wants to purchase, attempt to disable them.
    However, a Null Reference Exception error occurred. I want to check which part is wrong.

    The following is the content of the error.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6.  
    7. public class Store_Sky : MonoBehaviour
    8. {
    9.     public ItemBuffer itemBuffer;
    10.     public Transform slotRoot;
    11.  
    12.     public Action<ItemProperty> onClickBuy;
    13.  
    14.     private List<Slot> slots;
    15.     public Inventory inven;
    16.     //게임데이터 파일 만들면 삭제하기
    17.     int userStar = 4;
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         slots = new List<Slot>();
    22.         int slotCnt = slotRoot.childCount;
    23.  
    24.         for(int i = 0; i < slotCnt; i++)
    25.         {
    26.             var slot = slotRoot.GetChild(i).GetComponent<Slot>();
    27.  
    28.             if(slot.item.starNum <= userStar)
    29.             {
    30.                 slot.SetItem(itemBuffer.items[i+21]);
    31.             }
    32.             else
    33.             {
    34.                 slot.GetComponent<Button>().interactable = false;
    35.                 onClickBuy = null;
    36.             }
    37.  
    38.             slots.Add(slot);
    39.         }
    40.     }
    41.  
    42.     public void OnclickBuy(Slot slot)
    43.     {
    44.         if (userStar >= 0 && IsInven(slot) == false)
    45.         {
    46.             onClickBuy?.Invoke(slot.item);
    47.             userStar -= slot.item.starNum;
    48.             Debug.Log(userStar);
    49.         }
    50.     }
    51.  
    52.     private bool IsInven(Slot slot)
    53.     {
    54.         var invenList = inven.slots.FindAll(t =>
    55.         {
    56.             return t.item != null || t.item.itemname != string.Empty;
    57.         });
    58.  
    59.         if(invenList != null)
    60.         {
    61.             var isInven = invenList.Find(p =>
    62.             {
    63.                 return p.item.itemID == slot.item.itemID;
    64.             });
    65.  
    66.             return isInven != null || isInven.item.itemname != string.Empty;
    67.         }
    68.         else
    69.         {
    70.             return false;
    71.         }
    72.     }
    73. }
    74.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The compiler message tells you which part is wrong. It's line 56. Since the only object reference on that line is t, we can tell that t is null, i.e., your inven.slots list has some null references in it.
     
    Joe-Censored likes this.
  3. amp3670

    amp3670

    Joined:
    Apr 1, 2020
    Posts:
    10
    it works! Thanks alot!!
     
    JoeStrout likes this.