N
N
Nikita072019-01-30 18:51:33
.NET
Nikita07, 2019-01-30 18:51:33

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();
    }
}

But after execution, the program gives such an error 5c51c73ae694b325778755.png
. When deferring, an error occurs in this piece of code
public string Driver_Info(string name_driver, string surname_driver, string patronymic_driver)
{
string Full_Name_Driver;
driver.Name = name_driver;
driver.Name is null

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-01-30
@Nikita07

driver.Name is null

And type somewhere in the resulted code other value is exposed?
Failed just for null initialization of variables. Use String.Empty. Check the value of variables for null before trying to do something with them. Variable value == null - an exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question