Не создается объект класса - C#
Формулировка задачи:
Выдает ошыбку object reference not set to an instance of an object
вот код
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Book
{
string name;
string avtor;
string ganr;
int years;
public string Name
{
get
{
return name;
}
set {
name = value;
}
}
public string Avtor
{
get
{
return avtor;
}
set
{
avtor = value;
}
}
public string Ganr
{
get
{
return ganr;
}
set
{
ganr = value;
}
}
public int Years
{
get
{
return years;
}
set
{
years = value;
}
}
}
class Library {
Book[] books;
public Library()
{
books = new Book[10];
}
public void add_book(string name, string avtor, string ganr, int years)
{
this.books[0].Name = name;
this.books[0].Avtor = avtor;
this.books[0].Ganr = ganr;
this.books[0].Years = years;
////books[books.Length-1].Name = Console.ReadLine();
////books[books.Length-1].Avtor = Console.ReadLine();
////books[books.Length-1].Ganr = Console.ReadLine();
//books[0].Years = Convert.ToInt32(Console.ReadLine());
}
public int find_name(string name)
{
for (int i = 0; i < books.Length; i++)
{
if (books[i].Name == name) return i;
}
return -1;
}
public int find_avtor(string avtor)
{
for (int i = 0; i < books.Length; i++)
{
if (books[i].Avtor == avtor) return i;
}
return -1;
}
public int find_ganr(string ganr)
{
for (int i = 0; i < books.Length; i++)
{
if (books[i].Ganr == ganr) return i;
}
return -1;
}
public int find_avtor(int years)
{
for (int i = 0; i < books.Length; i++)
{
if (books[i].Years == years) return i;
}
return -1;
}
public void print(int pos) {
books[pos].Name = Console.ReadLine();
books[pos].Avtor = Console.ReadLine();
books[pos].Ganr = Console.ReadLine();
books[pos].Years = Convert.ToInt32(Console.ReadLine());
}
public int get_length() {
return books.Length;
}
}
class Program
{
static void Main(string[] args)
{
Library l = new Library();
Console.WriteLine("1 - Add book");
Console.WriteLine("2 - Delete book");
Console.WriteLine("3 - Find name");
Console.WriteLine("4 - Find avtor");
Console.WriteLine("5 - Find ganr");
Console.WriteLine("6 - Find years");
Console.WriteLine("7 - Print");
Console.WriteLine("8 - Exit");
int change = 0;
//int k;
//int pos;
while (change != 8)
{
change = Convert.ToInt32(Console.ReadLine());
switch (change)
{
case 1:
l.add_book("petr", "pako", "comedy", 1987);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
for(int i = 0; i < l.get_length(); i++){
l.print(i);
}
break;
case 8:
break;
}
}
}
}
}Решение задачи: «Не создается объект класса»
textual
Листинг программы
this.books[0] = new Book();