Ссылка на объект не указывает на экземпляр объекта:( - C#
Формулировка задачи:
Не могу понять в чем дело:
вот в этом месте ошибка:
в чем может быть дело?
namespace ConsoleApplication1 { class MyCollection { public static Animals[] animArr = new Animals[9]; static Random r = new Random(); public int Length { get; set; } public static void Create() { int i = 0; Mammals a1 = new Mammals("Кошка", "Муська", 3, "Хищные"); Mammals a2 = new Mammals("Дельфин", "Лори", 80, "Китообразные"); Mammals a3 = new Mammals("Тушканчик", "Туня", 0.1, "Грызуны"); Artiodactyls b1 = new Artiodactyls("Лашадь", "Стрела", 90, false); Artiodactyls b2 = new Artiodactyls("Корова", "Ночка", 100, true); Artiodactyls b3 = new Artiodactyls("Олень", "Иммануил", 150, true); Birds c1 = new Birds("Орел", "Боря", 2, 100); Birds c2 = new Birds("Синица", "Синька", 0.3, 40); Birds c3 = new Birds("Ворон", "Дрон", 2, 50); animArr[0] = a1; animArr[1] = a2; animArr[2] = a3; animArr[3] = b1; animArr[4] = b2; animArr[5] = b3; animArr[6] = c1; animArr[7] = c2; animArr[8] = c3; Console.WriteLine("Введите количество элементов массива:"); int numElementOfArr = Convert.ToInt32(Console.ReadLine()); if (numElementOfArr <= 0) { Console.WriteLine("Количество элементов не может быть отрицательным или равным нулю!"); } else if (numElementOfArr >= animArr.Length) { numElementOfArr = animArr.Length; Console.WriteLine("\nМаксимальное количество различных элементов в списке может быть только 9! :))"); } Animals[] temp1 = new Animals[numElementOfArr]; for (i = 0; i < numElementOfArr; i++) { temp1[i] = animArr[r.Next(animArr.Length)]; } animArr = temp1; for (i = 0; i < numElementOfArr; i++) Console.WriteLine("\n" + animArr[i]); //Console.WriteLine("\n" + animArr[r.Next(animArr.Length)]); } //public MyCollection() //{ // animArr = null; // Length = 0; //} public virtual void Add(Animals a) { //Animals[] temp = new Animals[this.Length + 1]; Animals[] temp = new Animals[this.Length+10]; int i = 0; foreach (Animals an in animArr) { temp[i] = an; i++; } temp[i] = a; Length = Length + 1; animArr = temp; } public virtual void Delete(int num) { Animals[] temp = new Animals[this.Length - 1]; int i = 0; int j = 0; foreach (Animals anim in animArr) { if (i + 1 != num) { temp[j] = anim; j++; } else { Console.WriteLine("Удаляется элемент "); anim.Show(); } i++; } Length = Length - 1; animArr = temp; } public void Show() { foreach (Animals a in animArr) a.Show(); } } }
a.Show();
class Program { static void Main(string[] args) { Mammals m1 = new Mammals("Панда", "Паня", 70, "Медвежьи"); Artiodactyls a1 = new Artiodactyls("Жираф", "Иваныч", 120, false); Birds b1 = new Birds("Пингвин", "Лола", 60, 0); MyCollection.Create(); Console.WriteLine("\nСоздали объекты"); Console.WriteLine("\nДобавляем элементы"); MyCollection MyColl = new MyCollection(); MyColl.Add(m1); MyColl.Show(); Console.ReadLine(); } }
Решение задачи: «Ссылка на объект не указывает на экземпляр объекта:(»
textual
Листинг программы
public void Show() { foreach (Animals a in animArr.Take(Length)) a.Show(); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д