Hi all.
I am unable to solve this error, I am checking for Null and cancelling if possible but still nothing, The error is:
The Code causing it is:Code:[Oxide] 13:27 [Error] Failed to call hook 'OnXpEarn' on plugin 'PvXselector v0.9.3' (NullReferenceException: Object reference not set to an instance of an object) [Oxide] 13:27 [Debug] at BasePlayer.get_ExperienceLevel () [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.OnXpEarn (UInt64 _userID, Single _xpValue, System.String source) [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 [Oxide] 13:27 [Error] Failed to call hook 'OnXpEarn' on plugin 'PvXselector v0.9.3' (NullReferenceException: Object reference not set to an instance of an object) [Oxide] 13:27 [Debug] at BasePlayer.get_ExperienceLevel () [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.OnXpEarn (UInt64 _userID, Single _xpValue, System.String source) [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 [Oxide] 13:27 [Error] Failed to call hook 'OnXpEarn' on plugin 'PvXselector v0.9.3' (NullReferenceException: Object reference not set to an instance of an object) [Oxide] 13:27 [Debug] at BasePlayer.get_ExperienceLevel () [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.OnXpEarn (UInt64 _userID, Single _xpValue, System.String source) [0x00000] in <filename unknown>:0 at Oxide.Plugins.PvXselector.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
I have tried everything I can think of, this error only seems to be caused on one server and after I see that error I find the Compiler is bugged, Not sure if they are related or if it crashes due to the massive amount of Debug spam from this error (im taling about 300 lines of that error message).Code:object OnXpEarn(ulong _userID, float _xpValue, string source){ if (_userID == 0) return _xpValue; if (isNPC(_userID)) return _xpValue; BasePlayer _player = basePlayerByID(_userID); if (_player == null) return (_xpValue*0); if (isplayerNA(_userID)){ if(_player.IsConnected()) LangMSG(_player, "NoXPModeNA"); return 0f;} if ((_player.ExperienceLevel < PvECap) && (InfoCache[_userID].mode == "pve")) return _xpValue * XPMultPerm(_player); else if ((_player.ExperienceLevel < PvPCap) && (InfoCache[_userID].mode == "pvp")) return _xpValue * XPMultPerm(_player); else return 0f;}
The admin believes it is caused from the XP share from crafting or something.
Solved Need help solving an error
Discussion in 'Rust Development' started by Alphawar, Sep 3, 2016.
-
Firstly, you should really format your code properly.
Secondly, _player.xp is likely null. -
Wulf Community Admin
Or is it _player.ExperienceLevel like the error suggests? -
The error suggests that the error occurs in BasePlayer.get_ExperienceLevel. .get_ExperienceLevel (as such, the .ExperienceLevel property) is actually just a function that calculates something (and yes, this is bullshit, because properties shouldn't be used for functions that calculate things), and that function uses BasePlayer.xp.
ExperienceLevel is not a property that can be null, it's a function that returns an int (and ints cannot be null either!). -
Feel free to make and recommendations, I only started doing this as a hobby and have very little knowledge, I dont event do this as a career. I work well I guess in Sales.
This entire statement made 0 sense to me, I am sorry but I dont understand what you mean.
I think K1llY0u may have provided a solution:
Code:object OnXpEarn(ulong _userID, float _xpValue, string source) { if (_userID == 0) return _xpValue; if (isNPC(_userID)) return _xpValue; if (isplayerNA(_userID)) return 0f; BasePlayer _player = basePlayerByID(_userID); if (_player == null) return _xpValue; var agent = GetPlayerLevel(_userID); if (agent != null){ if (((isPvP(_userID)) && ((int)agent < PvPCap)) || ((isPvE(_userID)) && ((int)agent < PvECap))) return _xpValue * XPMultPerm(_player); else return 0f;} else return 0f;} private object GetPlayerLevel(ulong _ID){ var agent = BasePlayer.FindXpAgent(_ID); if (agent != null){ return Math.Floor(agent.CurrentLevel);} return null;} -
C# Coding Conventions (C# Programming Guide)
Checking agent != null should roughly be the same as checking .xp != null, so yeah, that might solve the issue.
That message was a reply to Wulf's statement that .ExperienceLevel itself might be null, he'll know what I mean
Basically, the BasePlayer that causes the error has a null xp agent. Under the covers, _player.ExperienceLevel accesses that xp agent, and because it's null, it throws an error. -
Thank you.
I will try to follow coding conventions as much as possible, as I am normally coding when I should be sleeping and on a small screen I have been trying to make it as compact as possible.
Again thanks.
