NullReferenceException calling another plugin

I am new to C# so forgive me. I know other languages, but not C#.
I'm attempting to edit the Rename plugin to add some functionality. Specifically, check what Zone their in using ZoneManager and rename them depending on their zone.

However I keep getting a NullReferenceExcpetion when trying to call the ZoneManager api.

NullReferenceException: Object reference not set to an instance of an object

 

void OnUserConnected(IPlayer player)
        {
            BasePlayer baseplayer = player.Object as BasePlayer;
            bool IsPlayerInRPZone = ZoneManager.Call<bool>("IsPlayerInZone", "86165126", player); //rp zone
            if (IsPlayerInRPZone)
            {
                //do rp rename
            }
            else
            {
                //is in PVP, set pvp tag
            }

        }

After commenting out some lines, i know the error is being thrown on the .call line. Why would the api be trying to return an object, and even so, aren't I casting bool to it? How do I get this to work?

You need a plugin reference to ZoneManager and you need to null check it.

@wulf I do, at the top of my script there is

namespace Oxide.Plugins
{
    [Info("Rename", "Wulf/lukespragg", "0.3.0", ResourceId = 1184)]
    [Description("Allows players with permission to instantly rename other players or self")]

    class Rename : CovalencePlugin
    {
        #region Initialization
        [PluginReference] Plugin ZoneManager;

 

Why do I need to null check it? It should be returning a boolean, unless the api call is coded incorrectly

I'd suggest adding some debug output to find the null.

If ZoneManager isn't loaded for whatever reason, you will get an NRE when trying to utilize that reference for that call. If the above is what you actually have, then that or the response is the cause. Debug checks will help.

sUDB58ov5XtqBOX.jpg Wulf

I'd suggest adding some debug output to find the null.

If ZoneManager isn't loaded for whatever reason, you will get an NRE when trying to utilize that reference for that call. If the above is what you actually have, then that or the response is the cause. Debug checks will help.

The ZoneManager is null... Why would this be? Here's my complete code.

 

using System;
using System.Collections.Generic;
using System.Linq;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;

namespace Oxide.Plugins
{
    [Info("Rename", "Wulf/lukespragg", "0.3.0", ResourceId = 1184)]
    [Description("Allows players with permission to instantly rename other players or self")]

    class Rename : CovalencePlugin
    {
        #region Initialization
        [PluginReference] Plugin BetterChat; Plugin ZoneManager;
        StoredData storedData;
        const string permOthers = "rename.others";
        const string permSelf = "rename.self";

        bool persistent;
        bool preventAdmin;

        protected override void LoadDefaultConfig()
        {
            // Options
            Config["Persistent Renames (true/false)"] = persistent = GetConfig("Persistent Renames (true/false)", false);
            Config["Prevent Admin Renames (true/false)"] = preventAdmin = GetConfig("Prevent Admin Renames (true/false)", true);

            SaveConfig();
        }

        void Init()
        {
            LoadDefaultConfig();
            LoadDefaultMessages();
            LoadPersistentData();

            permission.RegisterPermission(permOthers, this);
            permission.RegisterPermission(permSelf, this);
        }

        #endregion

        #region Stored Data

        class StoredData
        {
            public readonly HashSet<PlayerInfo> Renames = new HashSet<PlayerInfo>();
        }

        class PlayerInfo
        {
            public string Id;
            public string Old;
            public string New;
            public string OriginalName;

            public PlayerInfo()
            {
            }

            public PlayerInfo(IPlayer player, string newName, string originalName)
            {
                Old = player.Name;
                Id = player.Id;
                New = newName;
                OriginalName = originalName;
            }
        }

        void LoadPersistentData()
        {
            storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Title);
        }

        void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Title, storedData);

        #endregion

        #region Localization

        void LoadDefaultMessages()
        {
            // English
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandUsageRename"] = "Usage: {0} <name or id> [new name] (new name only if renaming self)",
                ["CommandUsageReset"] = "Usage: {0} [name or id] (optional if resetting self)",
                ["NotAllowed"] = "You are not allowed to use the '{0}' command",
                ["PlayerIsAdmin"] = "{0} is admin and cannot be renamed",
                ["PlayerNameReset"] = "{0}'s name reset to {1}",
                ["PlayerNotFound"] = "{0} was not found",
                ["PlayerNotRenamed"] = "{0} is not renamed",
                ["PlayerRenamed"] = "{0} was renamed to {1}",
                ["YouWereRenamed"] = "You were renamed to {0}",
                ["YourNameReset"] = "Your name was reset to {0}"
            }, this);

            // French
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandUsageRename"] = "Utilisation : {0} <nom ou id> [nouveau nom] (nouveau nom uniquement si vous renommer)",
                ["CommandUsageReset"] = "Utilisation : {0} [nom ou id] (facultatif si la réinitialisation de soi)",
                ["NotAllowed"] = "Vous n’êtes pas autorisé à utiliser la commande « {0} »",
                ["PlayerIsAdmin"] = "Vous n’êtes pas autorisé à utiliser la commande « {0} »",
                ["PlayerNameReset"] = "Nom de {0} réinitialiser à {1}",
                ["PlayerNotFound"] = "{0} n’a pas été trouvée",
                ["PlayerNotRenamed"] = "{0} n’est pas renommé",
                ["PlayerRenamed"] = "A été renommé {0} {1}",
                ["YouWereRenamed"] = "Vous ont été renommées {0}",
                ["YourNameReset"] = "Votre nom a été réinitialisé à {0}"
            }, this, "fr");

            // German
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandUsageRename"] = "Verbrauch: {0} <Name oder Id> [neuer Name] (neuer Name nur, wenn Sie sich umbenennen)",
                ["CommandUsageReset"] = "Verwendung: {0} [Name oder Id] (optional, wenn selbst zurücksetzen)",
                ["NotAllowed"] = "Sie sind nicht berechtigt, verwenden Sie den Befehl '{0}'",
                ["PlayerIsAdmin"] = "{0} ist Admin und kann nicht umbenannt werden",
                ["PlayerNameReset"] = "{0} Namen auf {1} zurücksetzen",
                ["PlayerNotFound"] = "{0} wurde nicht gefunden",
                ["PlayerNotRenamed"] = "{0} wird nicht umbenannt",
                ["PlayerRenamed"] = "{0} wurde umbenannt in {1}",
                ["YouWereRenamed"] = "Sie wurden umbenannt in {0}",
                ["YourNameReset"] = "Ihr Name wurde auf {0} zurückgesetzt"
            }, this, "de");

            // Russian
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandUsageRename"] = "Использование: {0} <имя или id> [новое название] (новое имя только в случае переименования самостоятельно)",
                ["CommandUsageReset"] = "Использование: {0} [имя или id] (необязательно, если сброс самоуправления)",
                ["NotAllowed"] = "Нельзя использовать команду «{0}»",
                ["PlayerIsAdmin"] = "{0} является администратором и не может быть переименован",
                ["PlayerNameReset"] = "{0} сброс {1} имя",
                ["PlayerNotFound"] = "{0} не найден",
                ["PlayerNotRenamed"] = "{0} не переименован",
                ["PlayerRenamed"] = "{0} был переименован в {1}",
                ["YouWereRenamed"] = "Вы были переименованы в {0}",
                ["YourNameReset"] = "Ваше имя было сброшено в {0}"
            }, this, "ru");

            // Spanish
            lang.RegisterMessages(new Dictionary<string, string>
            {
                ["CommandUsageRename"] = "Uso: {0} <nombre o id> [nuevo nombre] (nuevo nombre sólo si cambiar el nombre a sí mismo)",
                ["CommandUsageReset"] = "Uso: {0} [nombre o id] (opcional en caso de reajuste del uno mismo)",
                ["NotAllowed"] = "No se permite utilizar el comando '{0}'",
                ["PlayerIsAdmin"] = "{0} es admin y no se puede cambiar",
                ["PlayerNameReset"] = "Nombre de {0} a {1}",
                ["PlayerNotFound"] = "No se encontró {0}",
                ["PlayerNotRenamed"] = "{0} no se cambia el nombre",
                ["PlayerRenamed"] = "Fue retitulado {0} a {1}",
                ["YouWereRenamed"] = "Sie wurden umbenannt in {0}",
                ["YourNameReset"] = "Su nombre fue reajustado a {0}"
            }, this, "es");
        }

        #endregion

        #region Connections

        void OnUserConnected(IPlayer player)
        {
            BasePlayer baseplayer = player.Object as BasePlayer;
            Puts($"baseplayer: {baseplayer}");
            Puts($"zonemanager: {ZoneManager}");

            bool IsPlayerInRPZone = ZoneManager.Call<bool>("IsPlayerInZone", "86165126", baseplayer); //rp zone
            Puts($"is player in zone: {IsPlayerInRPZone}");
            if (1==1)
            {
                //set rp name
            }
            else
            {
                //is in PVP, set pvp tag
            }

            // var rename = storedData.Renames.FirstOrDefault(r => r.Id == player.Id);
            // if (!persistent || rename == null) return;

            // player.Rename(rename.New);
            // Puts($"{rename.Old} was renamed to {rename.New}");
            // player.Message(Lang("YouWereRenamed", player.Id, rename.New));
            // var pos = player.Position();
            // player.Teleport(pos.X, pos.Y, pos.Z);
        }

        #endregion
        private void SetPVPTag() => BetterChat?.Call("API_RegisterThirdPartyTitle", new object[] { this, new Func<IPlayer, string>(GetPVPTag) });
        private string GetPVPTag(IPlayer player) => "[PVP]";

        #region Commands

        [Command("rename")]
        void RenameCommand(IPlayer player, string command, string[] args)
        {
            if (args.Length >= 2 && !player.HasPermission(permOthers) || !player.HasPermission(permSelf))
            {
                player.Reply(Lang("NotAllowed", player.Id, command));
                return;
            }

            if (args.Length == 0 || args.Length == 1 && player.Id == "server_console")
            {
                player.Reply(Lang("CommandUsageRename", player.Id, command));
                return;
            }

            var target = args.Length >= 2 ? players.FindPlayer(args[0]) : player;
            if (target == null)
            {
                player.Reply(Lang("PlayerNotFound", player.Id, args[0].Sanitize()));
                return;
            }

            if (args.Length >= 2 && preventAdmin && target.IsAdmin)
            {
                player.Reply(Lang("PlayerIsAdmin", player.Id, target.Name.Sanitize()));
                return;
            }

            var newName = args.Length >= 2 ? args[1].Sanitize() : args[0].Sanitize();
            target.Message(Lang("YouWereRenamed", target.Id, newName.Sanitize()));
            if (!Equals(target, player)) player.Reply(Lang("PlayerRenamed", player.Id, target.Name.Sanitize(), newName.Sanitize()));

            if (persistent)
            {
                var rename = storedData.Renames.FirstOrDefault(r => r.Id == target.Id);
                string originalName = "";
                if (rename == null)
                {
                    originalName = player.Name;
                    newName = newName + " (" + player.Name + ")";
                }
                else
                {
                    originalName = rename.OriginalName;
                    newName = newName + " (" + rename.OriginalName + ")";
                }
                storedData.Renames.RemoveWhere(r => r.Id == target.Id);
                storedData.Renames.Add(new PlayerInfo(target, newName, originalName));
                SaveData();
            }

            //target.Rename(newName);
            target.Rename(newName);
        }

        [Command("resetname", "namereset")]
        void NameResetCommand(IPlayer player, string command, string[] args)
        {
            if (args.Length >= 1 && !player.HasPermission(permOthers) || !player.HasPermission(permSelf))
            {
                player.Reply(Lang("NotAllowed", player.Id, command));
                return;
            }

            if (args.Length == 0 && player.Id == "server_console")
            {
                player.Reply(Lang("CommandUsageReset", player.Id, command));
                return;
            }

            var target = args.Length >= 1 ? players.FindPlayer(args[0]) : player;
            if (target == null)
            {
                player.Reply(Lang("PlayerNotFound", player.Id, args[0].Sanitize()));
                return;
            }

            var rename = storedData.Renames.FirstOrDefault(r => r.Id == target.Id);
            if (rename == null)
            {
                player.Reply(Lang("PlayerNotRenamed", player.Id, args[0].Sanitize()));
                return;
            }

            target.Message(Lang("YourNameReset", target.Id, rename.OriginalName.Sanitize()));
            if (!Equals(target, player)) player.Reply(Lang("PlayerNameReset", player.Id, target.Name.Sanitize(), rename.Old.Sanitize()));

            if (target.IsConnected) target.Rename(rename.OriginalName);
            storedData.Renames.Remove(rename);
            SaveData();
        }
        #endregion

        #region Helpers

        T GetConfig<T>(string name, T value) => Config[name] == null ? value : (T)Convert.ChangeType(Config[name], typeof(T));

        string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);

        #endregion
    }
}

You aren't setting the plugin reference.

Remove the extra ; Plugin and replace with a comma.