Answer the question
In order to leave comments, you need to log in
Why do I get the error "Object reference does not point to an instance of an object."?
Good evening, please tell me what is the error, you need to create two classes according to the task (all fields and constructors are created according to the task, more is not allowed) and display all the information on the screen
using System;
class Person
{
private string _name;
private string _surname;
private string _patronymic;
private int _age;
// Конструктор по умолчанию
public Person()
{
_name = null; _surname = null; _patronymic = null; _age = 0;
}
// Конструктор с 4 параметрами
public Person(string n, string s, string p, int a)
{
_name = n; _surname = s; _patronymic = p; _age = a;
}
// Установка возраста
public int Age
{
set
{
if (value >= 0 & value <= 200) _age = value;
else Console.WriteLine("Ошибка при вводе возроста");
}
get { return _age; }
}
// Установка имени
public string Name
{
set
{
if (value.Length < 20) _name = value;
else Console.WriteLine("Длина строки должна быть меньше 20 символов");
}
get { return _name; }
}
// Установка фамилии
public string Surname
{
set
{
if (value.Length < 20) _surname = value;
else Console.WriteLine("Длина строки должна быть меньше 20 символов");
}
get { return _surname; }
}
// Установка отчества
public string Patronymic
{
set
{
if (value.Length < 20) _patronymic = value;
else Console.WriteLine("Длина строки должна быть меньше 20 символов");
}
get { return _patronymic; }
}
// Метод для вывода информации
public void Person_Info()
{
Console.WriteLine("Фамилия: {0} \nИмя: {1} \nОтчество: {2} \nВозраст: {3}", Surname, Name, Patronymic, Age);
}
}
using System;
// Перечисление
enum Brand_Car { Ford }
class Car
{
private Person driver;
private Brand_Car brand;
private string number;
// Конструктор по умолчанию
public Car()
{
driver = null; brand = 0; number = null;
}
// Конструктор с 2 параметрами
public Car(Brand_Car b, string n)
{
brand = b;
number = n;
}
// Вывод марки автомобиля
public Brand_Car BrandCar
{
get { return brand; }
}
public Person PR { get; set; }
// Вывод номера автомобиля
public string Number_Car
{
get { return number; }
}
// Установка данных водителя
public string Driver_Info(string name_driver, string surname_driver, string patronymic_driver)
{
string Full_Name_Driver;
driver.Name = name_driver; driver.Surname = surname_driver; driver.Patronymic = patronymic_driver;
Full_Name_Driver = driver.Name + driver.Surname + driver.Patronymic;
return Full_Name_Driver;
}
// Установка возроста водителя
public int Driver_Age(int DriverAge)
{
if (DriverAge >= 18) driver.Age = DriverAge;
else
{
Console.WriteLine("Ошибка, водитель не может быть младше 18 лет");
return 0;
}
return driver.Age;
}
// Метод для вывода информации
public void Full_Driver_Info()
{
Console.WriteLine("Марка автомобиля: {0} \nНомер автомобиля: {1} \n\nФИО водителя: {2}", BrandCar, Number_Car, Driver_Info("ИМЯ","ФАМИЛИЯ","ОТЧЕСТВО"));
}
static void Main()
{
Person OBJ_Person = new Person();
Person OBJ_Person2 = new Person("ИМЯ","ФАМИЛИЯ","ОТЧЕСТВО",25);
OBJ_Person2.Person_Info();
Car OBJ_Car = new Car(Brand_Car.Ford, "A123BB");
OBJ_Car.Full_Driver_Info();
}
}
Answer the question
In order to leave comments, you need to log in
driver.Name is null
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question