我写的函数中有奇怪的空引用异常

时间:2012-02-05 20:15:45

标签: c# .net dictionary xmpp nullreferenceexception

private void GoogleTalkClient_OnMessage(object sender, jabber.protocol.client.Message msg)
    {
        connectionsNumber.Maximum = rosterManager.Count;
        if (session.Keys.Contains<string>(msg.From.Bare)){

            session.TryGetValue(msg.From.Bare,out thissession);

        }else{
            if (cleverbot.Keys.Contains<string>(msg.From.Bare)){
                cleverbot.TryGetValue(msg.From.Bare,out bot);
            }else{
                bot = factory.Create(ChatterBotType.CLEVERBOT);
                if (bot != null)
                cleverbot.Add(msg.From.Bare, bot);
            }
            thissession = bot.CreateSession();
            if (thissession != null)
            session.Add(msg.From.Bare, thissession);
        }
        connectionsNumber.Value = session.Count;
        jabber.protocol.client.Message reply = new jabber.protocol.client.Message(GoogleTalkClient.Document);
        reply.Body = thissession.Think(Grammarcheck(msg.Body));
        reply.To = msg.From;
        GoogleTalkClient.Write(reply);
    }

代码是C#,它在所有session.Add()和cleverbot.Add()语句中给出了null引用错误,我无法弄清楚为什么(cleverbot和session是dictionarys)

1 个答案:

答案 0 :(得分:0)

字典没有正确初始化