Home > Software design >  This piece of code gets an error that those variables haven't been assgined to, and I can'
This piece of code gets an error that those variables haven't been assgined to, and I can'

Time:11-16

I want to make a static List, but when I try read from file, and give the lists a value, it says that it haven't been assigned to, so its value is null.

class Log
    {   static public List<string> varos;
        static public List<int> tav;
        static public List<int> n;

        public void Input()
        {
            var sr = new StreamReader("vartav.txt");

            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                string[] seged = s.Split(' ');
                Log.varos.Add(seged[0]);
                Log.tav.Add(Convert.ToInt32(seged[1]));
            }
        } 

CodePudding user response:

A variable is not an object, it is simply a placeholder for an object. You are declaring the variables:

static public List<string> varos;

But you never initialized them to an object:

static public List<string> varos = new List<string>();

You could alternatively initialize them inside the method. Though of course they wouldn't be initialized until that method is invoked.

CodePudding user response:

try initializing inside Input() method

varos = new List<string>();
tav = new List<string>();
n = new List<string>();

or initializie when u declare em

static public List<string> varos = new List<string>();
static public List<string> tav = new List<string>();
static public List<string> n = new List<string>();
  • Related