Getting a NullReferenceException with some codeSolved
void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
        {
            BasePlayer killer = info?.Initiator as BasePlayer;

            

            if (entity == null | info == null) return;

            if (killer == null || killer == entity) return;




            if (entity.ShortPrefabName == "heavyscientist")
            {
                if ((bool)Config["Count Heavy Scientists"] == true)
                    if (Cachedplayerstats.ContainsKey(killer.userID)) Cachedplayerstats[killer.userID].heavyKills++;
                HeavyCount(killer);
            }
            if (entity.ShortPrefabName == "scientistnpc")
            {
                if ((bool)Config["Count Scientists"] == true)
                    if (Cachedplayerstats.ContainsKey(killer.userID)) Cachedplayerstats[killer.userID].scientistkills++;
                ScientistCount(killer);
            }

            if (entity.ShortPrefabName == "murderer")
            {
                if ((bool)Config["Count Murderers"] == true)
                    if (Cachedplayerstats.ContainsKey(killer.userID)) Cachedplayerstats[killer.userID].murdererkills++;
                MurdererCount(killer);
            }
            if (entity.ShortPrefabName == "scarecrow")
            {
                if ((bool)Config["Count Scarecrow"] == true)
                    if (Cachedplayerstats.ContainsKey(killer.userID)) Cachedplayerstats[killer.userID].scarecrowkills++;
               ScarecrowCount(killer);
            }




            
            foreach (var data in Cachedplayerstats) data.Value.Save(data.Key);
            return;



public void HeavyCount(BasePlayer player) 
        {                      
            if (!Cachedplayerstats.ContainsKey(player.userID)) PVEStatsData.TryLoad(player.userID);

            if (Cachedplayerstats[player.userID].heavyKills == (int)Config["first heavy scientist threshold"])
            {
                player.IPlayer.AddToGroup((string)Config["first heavy scientist threshold oxide group"]);
            }
            
            else if (Cachedplayerstats[player.userID].scientistkills == (int)Config["second heavy scientist threshold"])
            {
                player.IPlayer.AddToGroup((string)Config["second heavy scientist threshold oxide group"]);
            }

        }

I have a function that i made that will check if a player has reached a certain amount of ai kills and then reward them by adding them to a custom oxide group. However when its called, i get these errors:

Failed to call hook 'OnEntityDeath' on plugin 'NPCKillCounter v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
at Oxide.Plugins.NPCKillCounter.HeavyCount (BasePlayer player) [0x00045] in <2121e22bd2c04704a123578c5a0db5be>:0
at Oxide.Plugins.NPCKillCounter.OnEntityDeath (BaseCombatEntity entity, HitInfo info) [0x000a1] in <2121e22bd2c04704a123578c5a0db5be>:0
at Oxide.Plugins.NPCKillCounter.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00177] in <2121e22bd2c04704a123578c5a0db5be>:0
at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <d09a1f46ca2f4432811bcfe45ad13c7b>:0
at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <cf88a28c7fb44d36890d85a78331cc9d>:0
at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <cf88a28c7fb44d36890d85a78331cc9d>:0

Can someone help me fix it?

I have found the reason for the errors, it doesn't like it when i use this:
if (Cachedplayerstats[player.userID].heavyKills == (int)Config["first heavy scientist threshold"])​
but it will work if i use this:
if (Cachedplayerstats[player.userID].heavyKills == 5)​

I also tried setting the config to a variable first, but it didnt like that either. Any idea why it doesn't like the config part?

Sounds like it failed to cast to int, so the result was null. This would indicate that Config["first heavy scientist threshold"] is set to something that isn't castable to int, else doesn't exist.
You sure your using the correct veriable for the config? 
5e13a8d5b2bc5.jpg Wulf
Sounds like it failed to cast to int, so the result was null. This would indicate that Config["first heavy scientist threshold"] is set to something that isn't castable to int, else doesn't exist.

The config is set to:

Config["first heavy scientist threshold"] = 50;
I then tried setting it to a variable first, like this:
int heavy1 = (int)Config["first heavy scientist threshold"]​
but that didn't work either.
Seems like its not loading your config variables. check over your config code.
All of the other config items work fine, its just the ones that involve numbers that give me errors when i try to retrieve them.

Merged post

I completely changed the method i used for my config and have now fixed it. Thanks to everybody for the help :)
Locked automatically