C#C
C#16mo ago
17 replies
TimVibes

Null Literal and NUll reference problems

Hey guys,

Beginer at using Files here.

Im running into 2 errors while trying to read the contents of a text file, then putting the contents into a consructor.

"CS8600 Converting null literal or possible null value to non-nullable type.35

Warning (active) CS8603 Possible null reference return. 65"

i dont really understand what im missing tho.


        public static Employees[] EmployeeRead(string Filename)
        {
            try
            {
                FileInfo FileProperty = new FileInfo(Filename);
                Console.WriteLine($"File name: {FileProperty.Name}");

                FileStream file = new FileStream(Filename, FileMode.Open, FileAccess.Read);
                StreamReader text = new StreamReader(file);

                try
                {
                    Employees[] employees = new Employees[13];

                    string line;
                    int LineCount = 0;


                    while ((line = text.ReadLine()) != null) {

                        employees[LineCount] = new Employees(line, Convert.ToInt32(line[1]), Convert.ToDecimal(line[2]), Convert.ToDouble(line[3]));
                        LineCount++;
                    }


                    foreach (Employees employee in employees) {
                        employee.ToString();
                    }
                    text.Close();
                    return employees;


                } catch (InvalidCastException)
                {
                    Console.WriteLine("Some bad conversion");
                   
                   
                }catch (IOException)
                {
                    Console.WriteLine("idk");
                    
                }

            }catch (IOException)
            {
                Console.WriteLine("Smth wrong with the file path or reading file");
                
            }
            return null;
        }
Was this page helpful?