NullReferenceException at UpdateHostilityError

"plugin not working" unloaded

Failed to call hook 'CanHelicopterTarget' on plugin 'NudistHeli v0.0.9' (NullReferenceException: Object reference not set to an instance of an object)
  at Oxide.Plugins.NudistHeli.UpdateHostility (BasePlayer player, PatrolHelicopterAI heli) [0x000be] in :0
  at Oxide.Plugins.NudistHeli.CanHelicopterTarget (PatrolHelicopterAI heli, BasePlayer player) [0x00000] in :0
  at Oxide.Plugins.NudistHeli.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x0019e] in :0
  at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <80b90e8213db44b29ec2d4111764172c>:0
  at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in :0
  at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in :0

The fix for this is to add a null check for the activeItem and beltItem checks.  If the player is not holding an item it will be null, and that throws the exception.  Here is the corrected code:

if (settings.OnlyEngageOnWeaponHeld)
{
    var activeItem = player.GetActiveItem();

    if ((activeItem != null) && (settings.RestrictedWeapons.Contains(activeItem.info.shortname)))
    {
        if (settings.DebugMessages)
        {
            Puts($"Detected restricted held weapon for '{player.displayName}'.");
        }

        SetHostility(player, true);
    }
}
else
{
    foreach (var beltItem in belt)
    {
        if ((beltItem != null) && (settings.RestrictedWeapons.Contains(beltItem.info.shortname)))
        {
            if (settings.DebugMessages)
            {
                Puts($"Detected restricted belt weapon for '{player.displayName}'.");
            }

            SetHostility(player, true);
        }
    }
}
​