Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How To Declare An Array Of Lists Without NullReferenceException

Discussion in 'Scripting' started by BrandonK, Nov 21, 2015.

  1. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I declare it like this:
    Code (CSharp):
    1. public List<float>[] CMGHeight = new List<float>[25];
    yet whenever I try add, remove or do anything to a list in this array I get a null reference exception. I need it to be set at the deceleration so that I can access it through my editor script.

    I found another way to do it:
    Code (CSharp):
    1. // Run in editor script
    2. if (hd.CMGHeight[i] == null && !Application.isPlaying)
    3.                         {
    4.                             hd.CMGHeight[i] = new List<float>();
    5.                         }
    6.  
    7. hd.CMGHeight[i][x] = EditorGUILayout.FloatField(hd.CMGHeight[i][x]);
    This works perfectly in the editor, except if I enter playmode it sets it to a new list.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Each list needs to be new'd as well, not just the array of them
    Then theres the a chance that they dont serialize by default
     
    Solihin100 likes this.
  3. btft

    btft

    Joined:
    Aug 11, 2015
    Posts:
    14
    Code (CSharp):
    1. public List<float>[] CMGHeight = new List<float>[25];
    will build an array of size 25 of List<float> filled with nulls because it will initialize each element with default value.
    Since List<T> is a reference type, it will be intialized will null.
     
  4. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    Thanks I understand that each list in the array is null, but as you see in my code above it then sets it to a new list this works perfectly in the editor but when I enter play mode it goes back to null. The part of the code that sets it to a new list at that point in the array if it's null isn't run when I enter playmode so that can't be the problem, I can only assume that this:
    Code (CSharp):
    1. public List<float>[] CMGHeight = new List<float>[25];
    is for some reason is run every time I enter playmode creating a whole new array making everything null again. I declare it in the class outside of any functions.
     
  5. coward

    coward

    Joined:
    Jan 9, 2013
    Posts:
    21
    It sounds as though you may want to create a custom editor for a game object. If the array of lists exist on the game object, create the custom editor to access that list (and show the GUI). Then the gameobject's state will hang around when you go into play mode.
     
  6. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I have made a custom editor I've even tried using a button to set the list to a new one, for some reason it still sets to null when I enter playmode. I've found a work around although unfortunately it gives me other complications but I will revisit this later as I don't have a lot of time right now, but thank you everybody.
     
  7. btft

    btft

    Joined:
    Aug 11, 2015
    Posts:
    14
    How do you initialize those values then?
    I try to reproduce your error but can't even make inspector to work with array of lists or list of lists of simple types.
    To make it work in inspector I do this that way which doesn't fail:

    Code (CSharp):
    1.     public List<WrapperClass> list = new List<WrapperClass>(5);
    2.  
    3.     // Let's test things out in Start method
    4.     void Start ()
    5.     {
    6.         var tmp = 0;
    7.         foreach (var innerList in list)
    8.         {
    9.             foreach (var item in innerList.InnerList)
    10.             {
    11.                 if (item == 0)
    12.                 {
    13.                     Debug.LogError("Things got re set!");
    14.                     return;
    15.                 }
    16.             }
    17.         }
    18.         Debug.Log("Finished!");
    19.     }
    20.  
    21.     [Serializable]
    22.     public class WrapperClass
    23.     {
    24.         public List<int> InnerList;
    25.     }
    This causes extra boxing for your values but you probably might use it anyway for some additonal data.
     
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    edit: never mind