-1

I have this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zad_8.Chapter11
{
    class Program
    {
        static void Main(string[] args)
        {
             Cat[] cats = new Cat[10];
             for (int i = 0; i < 10; i++)
             {
                 cats[i].Name = "Cat " + sequence.NextValue();
             }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(cats[i].Name);
            }
        }
    }
}

this is my class in my namespace Chapter11

namespace Zad_8.Chapter11
    {
         public class Cat
        {
        private string name = "Cat";
        private string color;

         public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        public Cat(string name)
        {
            this.name = name;
        }
    }
} 

and this class also in my namespace Chapter11

namespace Zad_8.Chapter11
{
    public class sequence
    {
         private static int current = 0;

         private sequence()
        { }

    public static int NextValue()
       {
        current++;
        return current;
       }
    }
}

My goal is to create 10 object - Cat (type) and give them name like CatN, where N is their number.

I have class Cat + name property + get and set. I have class sequence and it will give me number from 1 to 10

In Main function I want to create array type Cat and with for loop to give on every element in this 10 index array The name CAT + Its number by sequence.

There is no errors in my error List, I tried lots of things like just printing 10 time Cat etc.. but every time it throws me this exception 'System.NullReferenceException.

I read in MSDN but didn't get most of the things good enough to overcome this problem.

I tried by (f11) debugging mode too, and seems like everything is fine but when it starts to read the line with "cats[i].Name = "Cat " + sequence.NextValue();" can move on and just stick on it and the compiler can't move on. I saw this in my array any of indexes is null and I'm not sure but it can be the problem.

Can anyone tell me how to do it right ?

I guess it's an annoying and boring question but I'm newbie and have to move through this, so please don't hate this post.

Puck
  • 2,080
  • 4
  • 19
  • 30
Stoyan Petkov
  • 481
  • 8
  • 26
  • 1
    `Cat` is a `class`, a `class` is a reference type. Reference types initialize with a `null` value. An array of reference types will be an array of `null` values. So for each `Cat`, you need a `new Cat`. – crashmstr Aug 29 '14 at 17:33

5 Answers5

1

When you're creating an array of cats

Cat[] cats = new Cat[10];

You're just creating an array which can contain cats. But there aren't actually any cats in there unless you put them in.

cats[i].Name = "Cat " + sequence.NextValue();

Over here you're asking for the 'ith' item in the array - which is null (no cats in there) and trying to do somethign with null. Which is what is causing your problem.

You need to actually put the cat in first.

    cats[i] = new Cat("Cat " + sequence.NextValue());
Ndech
  • 965
  • 10
  • 21
Haedrian
  • 4,240
  • 2
  • 32
  • 53
1

Change this:

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
  cats[i].Name = "Cat " + sequence.NextValue();
}

to this:

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
  cats[i] = new Cat(); //<------This line is new
  cats[i].Name = "Cat " + sequence.NextValue();
}

When you create your array it doesn't automatically instantiate (create) new objects. You need to specifically tell it to do that.

MikeH
  • 4,242
  • 1
  • 17
  • 32
0

cats[i] is null as the object constructor will put the default values in each new slot...which is null for a reference type. You cannot reference .Name of a null object

Create your cat object as a whole and plug it in as a whole.

What your updated code would like like:

cats[i] = new Cat("Cat " + sequence.NextValue());
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
0

Must create your Cat object first. replace your for loop with this:

for (int i = 0; i < 10; i++)
{
     cats[i] = new Cat("Cat " + sequence.NextValue());
}
Zeus82
  • 6,065
  • 9
  • 53
  • 77
0

You have not created an Object cat. You array is initialized with null values, as Cat is a reference type.

Cat[] cats = new Cat[10];
for (int i = 0; i < 10; i++)
{
    cats[i] = new Cat("Cat " + sequence.NextValue());
}
Ndech
  • 965
  • 10
  • 21