I get error : System.ArgumentNullException/ Value can not be null

Viewed 45

The startTime and endTime variables, which keep the times when I press the start and end buttons that I created on windows forms, give an error in the addrange function. If i define startTime as TimeSpan startTime = new TimeSpan(15, 0, 0); I got no problem.

public static List<ErrorLog> GetErrors(string filename, TimeSpan start, TimeSpan end)
            {.......}

void   function
    List<ErrorLog> errors = new List<ErrorLog>();
    errors.AddRange(ErrorLog.GetErrors(FILENAME, startTime, endTime));

button

private void b_start_Click(object sender, EventArgs e)
        {
            string tempDate = DateTime.Now.ToString("H:mm:ss");
            startTime = TimeSpan.Parse(tempDate);
        }
  • $exception {"Value cannot be null.\r\nParameter name: collection"} System.ArgumentNullException

Edit: GetErrors Added

public static List<ErrorLog> GetErrors(string filename, TimeSpan start, TimeSpan end)
            {
                List<ErrorLog> errors = null;
                XDocument doc = XDocument.Load(filename);
                List<XElement> logs = doc.Descendants("Logs").ToList();

                foreach (XElement log in logs)
                {
                    string errorStartTime = (string)log.Element("ErrorStartTime");
                    TimeSpan time = DateTime.ParseExact(errorStartTime, timeFormat, CultureInfo.InvariantCulture).TimeOfDay;

                    if (time >= start && time <= end)
                    {
                        ErrorLog error = new ErrorLog();
                        error.ID = (int)log.Attribute("ID");
                        error.errorGui = (string)log.Element("ErrorGUI");
                        error.errorType = (string)log.Element("ErrorType");
                        error.programPage = (string)log.Element("ProgramPage");
                        error.errorStartTime = time;
                        string strDate = (string)log.Attribute("Hour_Date");
                        error.date = DateTime.ParseExact(strDate, dateFormat, CultureInfo.InvariantCulture);
                        if (errors == null) 
                            errors = new List<ErrorLog>();
                        errors.Add(error);
                    }
                }
                return errors;
            }
0 Answers
Related