- Home /
 
Unable to use global Dictionary in function. "NullReferenceException"
Hi there!
I'm horribly stuck and have yet to find a solution, and appreciate it in advance any assistance the community can give me. Initially, the script used a list to store a class type 'Questions' which I am trying to create a dictionary so that I have a key->value pair. I call the dictionary in the MainController class shown below
 using System.Collections.Generic;
 
 public class MainController : MonoBehaviour
 {
         public Dictionary<int, Questions> GameQuestions;
 
 functions....
 
 }
 
               I then later use the GameQuestions dictionary in the LoadQuestions function as shown below:
 void Start()
     {
         //Invoke the load question coroutine
         if (Mode == DownloadMode.Offline)
             StartCoroutine(LoadQuestions());
         else if (Mode == DownloadMode.Online)
             StartCoroutine(DownlaodXML());
         else
             StartCoroutine(HybridLoader());
     }
 
 private IEnumerator LoadQuestions()
     {
         //Show the splash screen
         SplashScreen.SetActive(true);
 
         //Hide the retry button
         RetrySplashButton.SetActive(false);
 
         //Activate the splash screen loading animation and start playing it
         SplashScreenLoading.gameObject.SetActive(true);
         SplashScreenLoading.Play();
 
         //load the questions from the XML
         TKContainer ic = TKContainer.Load(OfflinePath);
         
         if (AllQuestions.Count > 0)
         {
             AllQuestions.Clear();
         }
 
         general.load();
 }
 
               When I'm trying to call GameQuestions.Count in the LoadQuestions IEnumerator, I receive the following error. 
The errror refers that I never create the GameQuestions variable, yet I have and doesn't seem to recognize it. Visual Studios never errors that I haven't created the variable yet, so I wouldn't know why it isn't being found. I at a lost and can't seem to figure it out. It worked perfectly when it was a list, however errors as a dictionary. The only differences between the two generics is that a dictionary isn't serializable, but I wouldn't think at all that would cause this issue. I have made GameQuestions as a local variable and it does work when it is in the function, however I need it to be a global variable as many other functions rely on it. Once again, thank-you for your assistance and I will provide more information if you need it.
Thank-you!
[1]: /storage/temp/96542-maincontroller.txt
To clarify: The error doesn't refer to not having created the GameQuestions variable. It refers to the GameQuestions variable being null, since it hasn't yet been assigned anything of type Dictionary<int, Questions>.
 If you assign it a new empty Dictionary<int, Questions>as indicated in @$$anonymous$$roq 's answer, it will not be null any more, and you can then start filling it with items.
Answer by EpsilonQoppa · Jun 24, 2017 at 10:01 PM
You haven't actually created the dictionary object. Try this:
 public Dictionary<int, Questions> GameQuestions = new Dictionary<int, Questions> ();
 
              Your answer
 
             Follow this Question
Related Questions
HELP!!! This NullReferenceException won't go away! :( 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Are C# Global Variables Safe In Unity? 5 Answers
Dictionary problems 1 Answer