0

I am currently working on a server system for a game called Reign of Kings. I am using hooks and commands given by the Oxide developers to make my server system. Back in the PAWN Language, we could do something like enumeration of player data inside an array, which converts it to a 2d array - something cool. like PlayerData[playerid][data] and data could be anything from an interger called pAdminLevel to a string called pPassword.

I get it that in C#, things are different. So I tried to replicate the method like this:

pData[] PlayerData = new pData[MAX_PLAYERS];
public class pData
{
    private int _admin;
    public int admin { get { return _admin; } set { _admin = value; } }
    public void ClearInfo()
    {
        _admin = 0;
    }
}

so basically whenever I want to call a player's name, I can use PlayerData[playerid].admin.

But I get the error:

5:13 PM [Error] Failed to call hook 'OnPlayerConnected' on plugin 'ServerCommands' (NullReferenceException: Object reference not set to an instance of an object)

After much testing I made it absolutely sure that the problem is infact the way I call PlayerData[x].admin and PlayerData[x].ClearInfo().

5
  • 1
    possible duplicate of What is a NullReferenceException and how do I fix it? ... depending on what playerid is, it may or may not be a valid array index. Commented May 20, 2015 at 14:47
  • You need to initialise the elements of playerData to instances of pData, they are null be default. Commented May 20, 2015 at 14:47
  • @Lee can you explain further? my knowledge of C# is not the best. Commented May 20, 2015 at 14:52
  • To give the right hint we first need to now what PlayerData is. A struct or a class? Commented May 20, 2015 at 14:56
  • pData[] PlayerData = new pData[MAX_PLAYERS] creates a new array of the required length, but assuming pData is a class, all of the array elements will be null references. You need to explicitly assign to them e.g PlayerData[0] = new pData(); Commented May 20, 2015 at 14:57

1 Answer 1

1

You're not initializing the members of your new array. Unlike in languages like C++, the contents of the array are reference types, and therefore your code is the C++ equivalent of creating an array of pointers and trying to use the members of the array right away.

Try something like this this instead:

pData[] PlayerData = new pData[MAX_PLAYERS];

for(int i=0; i < MAX_PLAYERS; i++)
{
    PlayerData[i] = new pData();
}

This will put a new pData object in each element of the array, and your code should then work as expected.

Sign up to request clarification or add additional context in comments.

3 Comments

new PlayerData() not new pData(). Is this todays StackOverFlow running gag to mix variables and classes? ;). Its the third question in the last half hour im seeing that.
@ralf: Looking at the OP's code above, they have a definition public class pData. Naming is a bit weird, but then it's not my code... :)
Yep that is the problem. Nice to learn a new thing in C#. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.