I use the dictionary to store user data. A timer is started that checks whether the user is online (how long ago did the user send a control packet saying that he is online).

A couple pops up an error

Value cannot be null. Parameter name: key

I can not understand what it is connected with.

Here is the code itself:

private void TimerFunc(object stat) { lock(this){ for (int i = 0; i < userTable.table.Count; i++) { try { var ud = userTable.table.ElementAt(i); if(ud.Key == null || ud.Value == null) { continue;//думал, что это как-то решит проблему } if (ud.Value.ip + ":" + ud.Value.port != serverData.IEPeP.ToString())//server не присылает нам пакет об онлайне, считается, что он онлайн всегда { Debug.WriteLine(ud.Value.ip + ":" + ud.Value.port); ud.Value.onlineTime -= 1000;//таймер запускает цикл каждую секунду if (ud.Value.onlineTime <= 0) { if(nickTable.table.ContainsKey(ud.Value.nick)) nickTable.table.Remove(ud.Value.nick); if(connectTable.table.ContainsKey(ud.Value.ip + ":" + ud.Value.port)) connectTable.table.Remove(ud.Value.ip + ":" + ud.Value.port); if(userTable.table.ContainsKey(ud.Key)) userTable.table.Remove(ud.Key); Debug.WriteLine("USER " + ud.Value.nick + " OFFLINE"); } else { userTable.table[ud.Key].onlineTime = ud.Value.onlineTime; } } } catch (Exception obj) { Debug.WriteLine("POINT 31+231: " + obj.Message);//вызывается это исключение } } } } 
  • one
    Well, put breakpoint in catch, and see which code throws an error. - VladD
  • I suspect that the userTable is a Dictionary or another collection that uses KeyValuePair as elements, then everything is correct, Key in KeyValuePair cannot be null under any conditions - rdorn
  • Not enough information. What type is userTable? On which line does the error go? What type is ud? - iRumba

1 answer 1

 if(ud.Value.nick != null && nickTable.table.ContainsKey(ud.Value.nick)) nickTable.table.Remove(ud.Value.nick); ... if(ud.Key != null && userTable.table.ContainsKey(ud.Key)) userTable.table.Remove(ud.Key); ... if(ud.Key != null) userTable.table[ud.Key].onlineTime = ud.Value.onlineTime;