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.