x


[Answered] NullReference problem with dictionary

Hi,

I'm having issues dealing with a dictionary that I want to use to keep track of what dialog has been said.

In both cases, the key doesn't exist in the dictionary. When I do this, things are fine.

    void Start(){
             Dictionary<int, string> saidDialogs = new Dictionary<int, int>();
             saidDialogs.TryGetValue(dIndex, out dictResult);
     }

But when I do this, I get constant NullReferenceExceptions:

     void Start(){
     Dictionary<int, int> saidDialogs = new Dictionary<int, int>();
     }
     
     void Update(){
     CheckForDialog();
     }
     
     void CheckForDialog(){
     saidDialogs.TryGetValue(dIndex, out dictResult);
     }

Any idea why?

more ▼

asked Feb 16 at 09:57 PM

kathode gravatar image

kathode
22 2 5 51

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Your dictionary should be a member of the class.

 class DialogThing {
 
       Dictionary<int, int> saidDialogs;
 
       void Start() {
           saidDialogs = new Dictionary<int,int>();
       }
 
 }

Basically the rest as you've got it should be fine from what I can see.

more ▼

answered Feb 16 at 10:20 PM

Wuzseen gravatar image

Wuzseen
533 45 47 55

(comments are locked)
10|3000 characters needed characters left

Ah thanks. Looks like the problem was that I was declaring the dictionary as a class member, and then in Start(), redeclaring it with "Dictionary saidDialogs = new Dictionary();"

Once I removed that "Dictionary" from the second part, things started working.

Thanks so much for all the help!

more ▼

answered Feb 16 at 10:33 PM

kathode gravatar image

kathode
22 2 5 51

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x777
x128

asked: Feb 16 at 09:57 PM

Seen: 182 times

Last Updated: Feb 16 at 10:55 PM