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().

Yuval Sade
  • 31
  • 1
  • 5
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/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. – Ňɏssa Pøngjǣrdenlarp May 20 '15 at 14:47
  • You need to initialise the elements of `playerData` to instances of `pData`, they are null be default. – Lee May 20 '15 at 14:47
  • @Lee can you explain further? my knowledge of C# is not the best. – Yuval Sade May 20 '15 at 14:52
  • To give the right hint we first need to now what PlayerData is. A struct or a class? – Ralf May 20 '15 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();` – Lee May 20 '15 at 14:57

1 Answers1

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.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
  • `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 May 20 '15 at 15:02
  • 1
    @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... :) – Baldrick May 20 '15 at 15:04
  • Yep that is the problem. Nice to learn a new thing in C#. Thanks! – Yuval Sade May 20 '15 at 15:15