0

I am trying to make a array out of object of the class called "Logistics", but when I do it, I get a "System.NullReferenceException" error.

public class Generate_Logistic_List
{

    Logistics[] logistic = new Logistics[2];

    public void Generate()
    {


        logistic[0].ChangeName("Test");


    }
}

What baffles me is that when I make it into a regular object of a class (i.e not making it an array) it works.

public class Generate_Logistic_List
{
    Logistics logistic = new Logistics();

    public void Generate()
    {           
        logistic.ChangeName("Test");
    }
}

Here is my "Logistics" class for reference.

public class Logistics
{
    protected string name;
    protected int time;
    protected int manpower;
    protected int ammo;
    protected int rations;
    protected int parts;


    public string Name(){
        return name;
    }

    public int Time(){
        return time;
    }

    public int Manpower(){
        return manpower;
    }

    public int Ammo(){
        return ammo;
    }

    public int Rations(){
        return rations;
    }

    public int Parts(){
        return parts;
    }

    public int TotalSupplies(){
        return manpower + ammo + rations + parts;
    }

    public double SupplyPerHour(int anySupply){
        double x = time / 60;
        return anySupply/x;
    }

    public void ChangeName(string newName){
        name = newName;
    }

    public void ChangeTime(int newTime){
        time=newTime;
    }


    public void ChangeSupplies(int newManpower, int newAmmo, int newRations, int newParts){
        manpower=newManpower;
        ammo=newAmmo;
        rations=newRations;
        parts=newParts;
    }
}

I have looked around the googles and to me it seems like my syntax for making the class into an array is okay.

If there would be a problem with my "Logistics" class I would presumable get an error when making a single object out of the class.

When I searched googles regarding array of classes the results seems to be about different problems, but since a relatively new to this I might have missed it thinking it wasn't relevant.

If that is the case I would be happy if somebody could redirect me to the post. I actually have no idea where else I could search for the answers to my problem, so any help would be very much appreciated.

5
  • 3
    Creating the array does not create the objects - you've just defined the array, type and length. Add logistic[0] = new Logistics(); before you assign values and it will work. Commented Aug 9, 2018 at 11:03
  • 2
    You did initialize the array instance, but you did not initialize the elements of the array. So logistics array is not null, but the element at logistics[0] is still null. Commented Aug 9, 2018 at 11:04
  • 1
    An Array is like a box. With Logistics[] logistic = new Logistics[2]; you create a box of Logistics that can hold up to 2 Logistics. When you logistic[0].ChangeName("Test"); you try to change the name of the first item in the box. but there is none. It's just a box. Maybe you have to fill the box first. logistic[0] = new Logistics();logistic[1] = new Logistics(); Commented Aug 9, 2018 at 11:06
  • If I had to Declare and Initialise an array of 100 item, I will var objArray = Enumerable.Range(1, 100).Select(x => new MyObject()).ToArray();. For simple short array I will go for one of those : stackoverflow.com/questions/5678216/… Commented Aug 9, 2018 at 11:12
  • If it's something you do often and want so clear syntax I will go for stackoverflow.com/a/4839502/6560478 Commented Aug 9, 2018 at 11:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.