-1

I've started learning C# and I have been following a few "mini projects" I found on the net and some I made up my self to help me understand the basics. This mini project requires me to create two classes that are named "item" and "inventory". The idea is that the item class is used to create items and the other inventory class is used to store the items and print them all. Here's the code so far:

class Program
    {
        static void Main(string[] args)
        {
            inventory my_inventory = new inventory();
            item cake = new item("Cake", 2.99, 001);
            item carrot = new item("Carrot", 0.59, 002);
            my_inventory.add_item(cake);
            my_inventory.add_item(carrot);
            my_inventory.print_inv();
            Console.ReadLine();
        }
    }


    class item 
    {
        string name;
        double price;
        int id;
        public item (string Name, double Price, int ID) 
        {
            this.name = Name;
            this.price = Price;
            this.id = ID;
        }
        public item() 
        {
            this.name = "unknown";
            this.price = 0.00;
            this.id = 000;
        }
        public override string ToString()
        {
            return "Name: " + name + " Price: " + price + " ID Number: " + id;
        }
    }

    class inventory 
    {
        object[] inv_list = new object[10];
        int tracker = 0;

        public void add_item(object obj) 
        {
            inv_list[tracker] = obj;
            tracker++;
        }

        public void print_inv() 
        {

            foreach ( object obj in inv_list) { Console.WriteLine(obj.ToString()); }
        }
    }

The error I keep running into is the "NullReferenceException" inside the print_inv() method and from what I have read it means that the object I'm trying to use on the print_inv() method is null? I'm not sure what this means in my code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Guy Clark
  • 13
  • 2

2 Answers2

1

The thing here is that when you create an array of something it's initialized with the default value for something. In case of object the default value is null.

So you need to modify you print_inv method to look through existing items:

public void print_inv() 
{
   for(int i =0; i < tracker; i++)
   {
      Console.WriteLine(inv_list[i].ToString());
   }
}
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
0

The issue is that since your declaring an array of a specific size (new object[10]) th earray is always that size. Therefore, when you iterate over it (foreach(object obj in inv_list) you're going to get everything, not just the values you've explicitly initialized. Since the default of object is null, then all but those explicit items out of your array are null.

There are a couple ways to fix this:

  • Replace foreach with for(int i = 0; i < tracker; i++) - this will only iterate through the items up to the tracker count, and no more.
  • Use a List<object> instead of an array. This will allow you to add/remove items without having to worry about capacity explicitly, and thus should avoid most auto-initialized values. May require more code to keep the inventory under 10 items, though.
  • Check for null and continue or break when you hit a null item in the inventory.
David
  • 10,458
  • 1
  • 28
  • 40
  • 1
    You are correct but it is better to suggest reading the canonical answer for NullReference Exceptions. See the duplicate link above – Steve Dec 24 '14 at 15:23