1. This is what my json looks like:

    Code:
    {
      "Players": {
        "76561198077945416": {
          "UserId": 76561198077945416,
          "TimeLeft": 610,
          "Multiple": true,
          "InitTimestamp": 0
        },
        "76561198154659830": {
          "UserId": 76561198154659830,
          "TimeLeft": 1280,
          "Multiple": true,
          "InitTimestamp": 0
        },
        "76561198115567857": {
          "UserId": 76561198115567857,
          "TimeLeft": 1430,
          "Multiple": true,
          "InitTimestamp": 0
        },
        "76561198216303485": {
          "UserId": 76561198216303485,
          "TimeLeft": 1290,
          "Multiple": true,
          "InitTimestamp": 0
        },
        "76561198130625124": {
          "UserId": 76561198130625124,
          "TimeLeft": 1800,
          "Multiple": false,
          "InitTimestamp": 0
        }
      }
    }
    
    Here's what my console looks like with the remove commented:
    [​IMG]

    This is the code you'll see in the console above:
    Code:
                foreach (var item in storedData.Players)
                {
                    if (item.Value.InitTimestamp == 0)
                    {
                        Puts("Removing " + item.Value.UserId.ToString() + " from the protection list due to script request.");
                        //storedDataEx.Players.Remove(item.Value.UserId);
                    }
                    else
                    {
                        DateTime compareDate = UnixTimeStampToDateTime(item.Value.InitTimestamp);
                        var days = (compareDate - DateTime.Now).Days;
                        if (days >= DaysCheck)
                        {
                            Puts("Removing " + item.Value.UserId.ToString() + " from the protection list due to inactivity (" + days.ToString() + " days).");
                            //storedData.Players.Remove(item.Value.UserId);
                        }
                    }
                }
    
    Now when I uncomment the remove line I get this error:
    [​IMG]

    Code:
    [Oxide] 6:02 AM [Error] Failed to call hook 'OnServerInitialized' on plugin 'StartProtection v1.4.0' (NullReferenceException: Object reference not set to an instance of an object)
    [Oxide] 6:02 AM [Debug]   at Oxide.Plugins.StartProtection.RemoveOldUsers () [0x00000] in <filename unknown>:0
      at Oxide.Plugins.StartProtection.OnServerInitialized () [0x00000] in <filename unknown>:0
      at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
      at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
    The odd part is that it actually removes one before this error occurs, I'm assuming it's something to do with running it within foreach? Any ways around this?
     
  2. Well I don't know much about c# but the error is similar to Python's, to which I mean it happens when you're trying to do something to an object which does not exist in a certain object, thats why the error states:
    My guess here is that item.Value.UserId returns an Integer and not a String as it should, so I would try this line instead:
    Code:
    storedDataEx.Players.Remove(Convert.String(item.Value.UserId))
    Though I am not sure if this is the correct code to convert an Integer to a String.

    But give it a try :)
     
  3. The value isn't an integer though, it's ulong
     
  4. Either way, the idea goes around to the same thing, try to convert it to string.
     
  5. Delete need more likely Key instead of the Value or not?
     
  6. emu

    emu

    You cannot delete elements from a collection in a foreach. You must use an iterator or make a copy of your list and use that in the foreach.
     
  7. This. Think of it as a for (int i= i; i < list.count; i++) loop. If you remove items during the loop from "list" here, you will go past the end of the list by as many elements as you removed.

    Best way is to use a reverse for i loop, this way this error cannot occur:

    for (int i= list.count-1; i >=0; i--)
    {
    list.delete(i);
    }

    This will work because the items you delete only affect the part of the array you already traversed.
     
  8. That was my suspicion, cheers.