Object not referenced (list inside a struct) c#

15 Views Asked by At

i am doing my coding mock final exam and ran into a problem. When I try to fill up a list from a text file with StreamReader, it works (at leasat i think it does). But when I try to access the list in a different method, it throws the Object reference error. If I reference it like this (the commented part) I will get back empty lists. I would appreciate all the help.

struct Adatok
        {
            public int sorszam;
            public List<string> vasarlasok;
        }
        static Adatok[] tomb = new Adatok[1000];
        static int index = 1;

        static void Beolv2()
        {
            StreamReader sr = new StreamReader("penztar.txt");
            StreamWriter sw = new StreamWriter("osszeg1.txt");

            while (!sr.EndOfStream)
            {
                tomb[index].vasarlasok = new List<string>();
                string sor = sr.ReadLine();

                if (sor != "F")
                {
                    tomb[index].vasarlasok.Add(sor);
                }
                else
                {
                    index++;
                }
                tomb[index].sorszam = index;
            }
            sr.Close();
            sw.Close();
        }

        static void f8()
        {
            Console.WriteLine("f8");
            StreamWriter sw = new StreamWriter("otszaz1.txt");

            for (int i = 0; i < length; i++)
            {
                //tomb[i].vasarlasok = new List<string>();
                sw.WriteLine($"{index}: {ertek(tomb[index].vasarlasok.Count)}");
            }
        }

I tried everything I knew about, but those attempts almost always resulted in this same problem

0

There are 0 best solutions below