0

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;
  • The line numbers don't always match up exactly. It seems clear that `Diseases` is uninitialized and thus null, so trying to index it gives you this exception. – Kirk Woll Jan 06 '22 at 23:59
  • Show us the line where you have written eg `diseases = new string[100]` – Caius Jard Jan 07 '22 at 00:02

0 Answers0