2012-02-05 38 views
2
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()語句的nullreference錯誤,我不明白爲什麼(機靈和會話是dictionarys)奇怪的空引用異常我寫了一個函數

+1

要麼你忘了初始化'cleverbot'和'session',或者你正在傳遞是空的數據。使用調試器瀏覽代碼,看看它是哪一個。 – 2012-02-05 20:17:48

+0

您是否正確初始化了字典?一個人只能根據這段代碼判斷一個可能的解決方案並不能說太多。 – DotNetStudent 2012-02-05 20:18:39

+0

你必須用'Dictionary session = new Dictionary ();'和'Dictionary clerverbot = new Dictionary ();'來初始化它們。這個'Dictionary session;'不會創建一個字典,它將是空的。 – 2012-02-05 20:23:29

回答

0

的字典中沒有正確的初始化