2 years ago

#21180

test-img

miisterichka

'Object reference not set to an instance of an object.' when using an array C#

I have created a class in this way:

public class patenta
    {
        private string[] diseases;
        public string[] Diseases
        {
            get;
            set;
        }
}

In my main program I have this; it shows me System.NullReferenceException: 'Object reference not set to an instance of an object.' right after the 'for' loop. How do I fix it?

patenta[] paciento = new patenta[100];
int j = 0;
int dis;
string t = Console.ReadLine();
while (t!="stop")
{
    switch(t)
    {
        case "pt":
        {
            paciento[i] = new patenta();
            Console.WriteLine("Please enter amount of diseases...");
            dis = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter names of diseases...");
            for (int pl=0; pl<dis; pl++)
            {
                paciento[j].Diseases[pl] = Console.ReadLine();
            }
            j++;
        } break;

c#

arrays

class

nullreferenceexception

0 Answers

Your Answer

Accepted video resources