0

This is the second time I've seen this error message today. I got some help earlier on, but now it seems to be happening with a date comparison function that I have written.

Whenever the user presses the Overdue button, all of the rows in the datagridview need to by updated by column 1 (DatePaid).

enter image description here

Unfortunately this doesn't seem to be working and I get the error message:

An unhandled exception of type 'System.NullReferenceException' occurred in SpeedyRent.exe

Additional information: Object reference not set to an instance of an object.

at:

DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());

enter image description here

My code is:

    public void viewOverdue_Click(object sender, EventArgs e)
    {
        viewOverdue.ForeColor = Color.Red;
        viewHire.ForeColor = Color.Black;
        viewRent.ForeColor = Color.Black;

        DateTime overdueDate = default(DateTime);
        DateTime today = DateTime.Now;
        string odDate = null;

        if (today.DayOfWeek == DayOfWeek.Monday)
        {
            overdueDate = today.AddDays(-12);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Tuesday)
        {
            overdueDate = today.AddDays(-13);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Wednesday)
        {
            overdueDate = today.AddDays(-7);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Thursday)
        {
            overdueDate = today.AddDays(-8);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Friday)
        {
            overdueDate = today.AddDays(-9);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Saturday)
        {
            overdueDate = today.AddDays(-10);
            odDate = overdueDate.Date.ToString("dd/MM/yyyy HH:mm:ss");
        }
        else if (today.DayOfWeek == DayOfWeek.Sunday)
        {
            overdueDate = today.AddDays(-11);
            odDate = overdueDate.Date.ToString("dd/MM/yyy HH:mm:ss");
        }

        CurrencyManager manager = (CurrencyManager)BindingContext[dataGridView1.DataSource];
        manager.SuspendBinding();
        DateTime dateBase = DateTime.Parse(odDate);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DateTime dateRow = DateTime.Parse(row.Cells[0].Value.ToString());
            row.Visible = (dateRow <= dateBase);
        }
        manager.ResumeBinding();
    }
Community
  • 1
  • 1
methuselah
  • 12,766
  • 47
  • 165
  • 315
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 18 '14 at 15:33
  • Have you tried running this in debug mode? It will stop on the line where the error occurs and you can inspect the all the variables and there values. – jensendp Apr 18 '14 at 15:39
  • 1
    To make it easier for you, this is where your error is occurring: `row.Cells[0]`. One of the objects you are retrieving from the Cells[] array is _null_. This means that your object does not point to any object in the memory (heap). So, at that point the object you are retrieving does not have a `Value` property and your code throws an exception. – user3439065 Apr 18 '14 at 15:42
  • 1
    I forgot to mention that your exception could also be that `Value` is `null`, in that case `ToString()` is causing an exception. You need to track your code and see where the problem is exactly. While debugging your program, expand the `row` variable in your Locals window. Now look if index 0 (`Cells[0]`) actually contains the information you need. If `Cells[0]` is _null_, then this is where your problem is. – user3439065 Apr 18 '14 at 15:58

0 Answers0