-2

I am trying to create 2 arrays which will be string arrays for each team.

I create this class

public class MyTeamWeapons
{
     public string[] weapons;
}

 public class MyLoadout
 {
     public MyTeamWeapons[] teamWeapons;
     public string[] playerModel;
 }

I then try to create an instance and initialize the array with length of 5 filled will dummy data.

 loadout = new MyLoadout();
 loadout.teamWeapons = new MyTeamWeapons[2];
 loadout.teamWeapons[0].weapons = new string[5] {"hellosd", "yous", "ds", "ad", 
"dd"};

I get:

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

When trying to initialize new string array of 5. I do not understand why. All I want is 2 teams and each team have an array of strings to hold weapons names.

Any help would be great, thanks!

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
noobCoder
  • 181
  • 2
  • 11

3 Answers3

1
loadout.teamWeapons = new MyTeamWeapons[2];
loadout.teamWeapons[0] = new MyTeamWeapons();
// or new() in C#9+
loadout.teamWeapons[0].weapons = new[] {"hellosd", "yous", "ds", "ad", "dd"};

and you must add a ? after each statement like this:

public string[]? weapons;

and

public MyTeamWeapons[]? teamWeapons;

what is ? : it say to compiler that variable can be null

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
MoRe
  • 2,296
  • 2
  • 3
  • 23
  • 2
    @noobCoder: MoRe is correct. You initialized the `MyTeamWeapons[] teamWeapons` array (good!) ... but you forgot to initialize either of the two MyTeamWeapons objects in that array (whoops!) Hence the null reference exception. MoRe also pointed out that you can (optionally!) make it [nullable](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types) (with the "?" syntax). Finally, as others point out below, you should probably use something like a List or Dictionary instead of arrays. – paulsm4 Apr 13 '22 at 23:12
  • @paulsm4 it is an comprehensive answer, not comment. please edit my answer, at least :) – MoRe Apr 13 '22 at 23:24
  • Q: Are you *complaining* that I *praised* your answer (and also "upvoted" it)??? – paulsm4 Apr 14 '22 at 03:40
  • @paulsm4 No, Certainly No, but I think your comment (because of comprehensiveness) can be part of answer and can complete my answer, instead of a comment :) because I didnt Add any description to my answer – MoRe Apr 14 '22 at 11:48
0

Getting NullReferenceException: Object reference not set to an instance of an object means that you missed an instantiation of an object which I think relates to loadout.

Lists (List) are favored over other data structures because of their ability to accommodate a wide range of data types. Lists use an array internally and should perform just as quickly as arrays

I tried to refactor with a little clear style as per below:

public class MyTeamWeapons
{
    public List<string> Weapons { get; set; }
}

public class MyLoadout
{
    public List<MyTeamWeapons> TeamWeapons { get; set; } = new();
    public string PlayerModel { get; set; }
}

And then you can use it without explicit arrays:

    static void Main(string[] args)
    {
        MyLoadout loadout1 = new();

        loadout1.PlayerModel = "Player Model";
        loadout1.TeamWeapons.Add(new MyTeamWeapons
        {
            Weapons = new List<string> { "hellosd", "yous", "ds", "ad", "dd" }
        });

        MyLoadout loadout2 = new();

        loadout2.PlayerModel = "Player Model 2";
        loadout2.TeamWeapons.Add(new MyTeamWeapons
        {
            Weapons = new List<string> { "Foo", "Foo2" }
        });
    }

More information regarding NullReferenceException

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23
0

I am recommending using Dictionary with List which provides you with much more flexibility and is easy to use and you will get so much functionality using them, here is my example, and please let me know if you don't understand or have any difficulties:

Dictionary<string, List<String>> teams = new Dictionary<string, List<String>>();

List<String> team1Wep = new List<String>();
team1Wep.Add("Weapons 1");
team1Wep.Add("Weapons 2");
team1Wep.Add("Weapons 3");
team1Wep.Add("Weapons 4");
team1Wep.Add("Weapons 5");

List<String> team2Wep = new List<String>();
team2Wep.Add("Weapons 11");
team2Wep.Add("Weapons 22");
team2Wep.Add("Weapons 33");
team2Wep.Add("Weapons 44");
team2Wep.Add("Weapons 55");

teams.Add("team1", team1Wep);
teams.Add("team2", team2Wep);

Console.WriteLine("This was a test");

more information about dictionary

**Debug image: ** Debug Image

AwatITWork
  • 556
  • 1
  • 5
  • 13