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. Dismiss Notice

[C#] Loading Data from a list returns Null Reference Exception

Discussion in 'Scripting' started by Rotavele, Aug 4, 2016.

  1. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    Hello Guys,

    What I am trying to do is load Data from a parsed CSV file that has had it's contents put into a list. I've attempted to band aid over this a bit to get it to work as I only need it to run once, but it's not working out.

    This is my code:

    Editor that calls it:
    Code (CSharp):
    1.  
    2.   [MenuItem("Place Managers/Grab from DB")]
    3.   private static void DBGrab()
    4.   {
    5.   GameObject Map = GameObject.Find("PlaceMap");
    6.   PlaceTable PlaceDB = GameObject.Find("PlaceDB").GetComponent<PlaceTable>();
    7.   PlaceScript[] Places;
    8.  
    9.   Places = Map.GetComponentsInChildren<PlaceScript>();
    10.  
    11.   //PlaceDB.Load(PlaceDB.CSV);
    12.  
    13.   if (PlaceDB.IsLoaded())
    14.   {
    15.   Debug.Log("Places are Loaded.");
    16.   }
    17.  
    18.   int Rows = PlaceDB.NumRows();
    19.  
    20.   Debug.Log("Number of Rows:" + Rows);
    21.  
    22.  
    23.   if (Places != null)
    24.   {
    25.   Debug.Log("Found Places, Attempting to Load New Data");
    26.   foreach (PlaceScript Region in Places)
    27.   {
    28.   int PlaceID = Region.PlaceID;
    29.   PlaceTable.Row HiRow = PlaceDB.GetAt(PlaceID);
    30.  
    31.   if(HiRow != null)
    32.   {
    33.   Region.Name = HiRow.Name;
    34.   Region.Population = int.Parse(HiRow.Population);
    35.   Region.gameObject.name = HiRow.Name;
    36.   } else
    37.   {
    38.   Debug.Log("Error Loading Data for:" + Region.Name + " of " + Region.State + ". Their Place ID is:" + Region.PlaceID);
    39.   }  
    40.   }
    41.   }
    42.   else
    43.   {
    44.   Debug.Log("Couldn't find Place scripts");
    45.   }
    46.   }
    47.  

    This is the Table Script. It has already loaded the CSV into the list and is returning the correct number of Rows on Debug. For some reason I just dont get any data at all when trying to access the nested class "Row", and if I attempt to use a PlaceScript.Row it returns a Null Reference exception.

    **THIS IS NOT MY WORK** All credits for this go to Yoon ChangSik's "CSV2Table" which is a free asset on the Unity Store. It generated this script for me and it's highly appreciated.

    Code (CSharp):
    1. public class PlacesTable : MonoBehaviour
    2. {
    3.     public class Row : MonoBehaviour
    4.     {
    5.         public string id;
    6.         public string Name;
    7.         public string State;
    8.         public string Population;
    9.     }
    10.  
    11.     public TextAsset CSV;
    12.     List<Row> rowList = new List<Row>();
    13.     bool isLoaded = false;
    14.  
    15.  
    16.  
    17.     public bool IsLoaded()
    18.     {
    19.         return isLoaded;
    20.     }
    21.  
    22.     public List<Row> GetRowList()
    23.     {
    24.         return rowList;
    25.     }
    26.  
    27.     public void Load(TextAsset csv)
    28.     {
    29.         rowList.Clear();
    30.         string[][] grid = CsvParser2.Parse(csv.text);
    31.         for(int i = 1 ; i < grid.Length ; i++)
    32.         {
    33.             Row row = new Row();
    34.             row.id = grid[i][0];
    35.             row.Name = grid[i][1];
    36.             row.State = grid[i][2];
    37.             row.Population = grid[i][3];
    38.  
    39.             rowList.Add(row);
    40.         }
    41.         isLoaded = true;
    42.     }
    43.  
    44.     public int NumRows()
    45.     {
    46.         return rowList.Count;
    47.     }
    48.  
    49. //<Disregard this, It was my attempt at a band-aid which also did not work>
    50.     public string[] GetDataArray(int i)
    51.     {
    52.         string[] Data = new string[4];
    53.         Row DataClass = GetAt(i);
    54.         Data[0] = DataClass.id;
    55.         Data[1] = DataClass.Name;
    56.         Data[2] = DataClass.State;
    57.         Data[3] = DataClass.Population;
    58.         return Data;
    59.     }
    60. // </End Disregard>
    61.  
    62.     public Row GetAt(int i)
    63.     {
    64.         if(rowList.Count <= i)
    65.             return null;
    66.         return rowList[i];
    67.     }
    68. }
     
  2. flotschie

    flotschie

    Joined:
    Jul 24, 2015
    Posts:
    20
    First, code formatting would help a lot to read your first code fragment. I don't know which editor you use, but in Visual Studio it would be a simple shortcut like Ctrl+K+D.

    Additionally it would help a lot if you also give us the exact information in which line exactly does the error appear (line number in the posted code fragment of course).

    And I see a lot of threads where the OP seemingly does not know about how to use Break Points. It would help a lot imho.

    In your case, I only can guess that probably the value of the index you hand over to the GetAt call is maybe out of the range of the rowList collection? Would be easily to find out by using Break Points or writing the value to the Log.
     
  3. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    My apologies as I was a bit scatter brained when posting this. It returns that a row has been found and the NullReference is when I call line 33 (Trying to access the row that I retrieved). That's what really threw me off.
     
  4. flotschie

    flotschie

    Joined:
    Jul 24, 2015
    Posts:
    20
    That's weird - if Region was null you should already get the error in line 28, and HiRow can also not be null due to the check before. Could you please post the exact error message maybe?
     
  5. Rotavele

    Rotavele

    Joined:
    Jan 28, 2016
    Posts:
    29
    That's exactly what I was thinking: it defies explanation for me too. Region can't be null either because I set to check if places was null first and then it just loops in a foreach of Places. The variables are all there because I've looped through them all on my other scripts doing various tasks and cleaning up some formatting.

    As for the exact error message: I have already re-did this by loading the CSV into my classes at the same time as it loads.

    Tonight I will write it back how I had it because I really want to know what's going on here for futures sake. The only thing I can think of is maybe since it's a nested class and My editor script is static: I can't use "PlaceTable.Row" and need to use "PlaceDB.row". Yet when I did that I had another error saying I couldn't access it from the PlaceDB instance.