- Home /
 
 
               Question by 
               urhokekkonen5 · Apr 07, 2017 at 06:46 AM · 
                editorcustom editorcustom-inspector  
              
 
              Null reference exceptions when using a Custom Editor
Hey,
I have this class:
 public class NarrativeMap : MonoBehaviour { 
 
     public LocalizationManager localizationManager;
     public Narrative[] narratives = new Narrative[0];
 
     ...
 }
 
               which contains an array of references to a custom Narrative class. Narrative looks something like this:
 [System.Serializable]
 public class Narrative
 {
     private string id;
     private string xmlPath;
     private List<KeyValuePair<string, AudioClip>> audioClips;
 
     public Narrative(LocalizationManager mgr)
     {
         audioClips = new List<KeyValuePair<string, AudioClip>>();
         id = string.Empty;
         xmlPath = string.Empty;
 
         ...
 
     }
     ...
 
     private bool langExistsInClips(string langId)
     {
         foreach (var clip in audioClips)
         {
             if (clip.Key == langId)
                 return true;
         }
 
         return false;
     }
     ...
     
 }
 
               The problem I am facing is that when I use the custom editor for NarrativeMap to modify and create new instances of the Narrative class and I call the various methods that do operations on the audioClips list, I get weird NullReference exceptions which basically state that the audioClips list in Narrative is not initialized. I don't understand how this is possible, as the list is initialized in the constructor and there should never be a case where the list is not initialized after the constructor has been called.
A snippet of the custom editor is here:
 [CustomEditor(typeof(NarrativeMap))]
 [CanEditMultipleObjects]
 public class NarrativeMapEditor : Editor {
 
 public override void OnInspectorGUI()
     {
         NarrativeMap narrativeMap = (NarrativeMap)target;
 
         narrativeMap.localizationManager = (LocalizationManager)EditorGUILayout.ObjectField("Localization manager", narrativeMap.localizationManager, typeof(LocalizationManager), true);
 
         int newLength = EditorGUILayout.IntField("Narrative count", narrativeMap.narratives.Length);
         newLength = newLength < 0 ? 0 : newLength;
 
         if (newLength < narrativeMap.narratives.Length)
         {
             Array.Resize(ref narrativeMap.narratives, newLength);
         }
         else if (newLength > narrativeMap.narratives.Length)
         {
             int origLength = narrativeMap.narratives.Length;
             Array.Resize(ref narrativeMap.narratives, newLength);
 
             for(int i = origLength; i < newLength; i++)
             {
                 narrativeMap.narratives[i] = new Narrative(narrativeMap.localizationManager);
             }
         }
 
         ...
 
         foreach (var narrative in narrativeMap.narratives)
         {
 
             narrative.updateClipList(narrativeMap.localizationManager);
 
             ...
 
         }
 
               Sorry for the flood of code and thank you very much for any help.
               Comment
              
 
               
              Your answer