This is what my json looks like:
Here's what my console looks like with the remove commented: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 } } }
This is the code you'll see in the console above:
Now when I uncomment the remove line I get this error: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); } } }
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?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
Error when removing from within foreach() c#
Discussion in 'Rust Development' started by Norn, Sep 28, 2015.
-
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:
Code:storedDataEx.Players.Remove(Convert.String(item.Value.UserId))
But give it a try -
The value isn't an integer though, it's ulong
-
-
Delete need more likely Key instead of the Value or not?
-
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.
-
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. -