Question

I am currently getting this error on a block of code. Please comment or offer suggestions how to solve this problem. Thank you.

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

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

The problem is simple, before passing the editRecord variable cast as ManualTime I get a null when it should have the selected record values from the listbox which are there. I tested the debugger and it reads null even though it was a list record constructed from an override from the same ManualTime class to cast it to from the listBox. This is hard to explain, what might be going on? Why the disconnect to write the properties to the editor form that follows with this variable? The constructor in the editor form fails due to this variable instance is a null reference. The code works until the final casting of the data structure which is strange.

private void editBtn_Click(object sender, EventArgs e)
        {
            //NEEDS SOME DEBUGGING
            //Choose a record and select in listbox

            if (listHoursLog.SelectedIndex >= 0)
            {
                var selection = listHoursLog.SelectedItem.ToString();
                    //Identify manually entered records from timer records that begin with "M"
               if (listHoursLog.SelectedIndex >= 0 && selection.StartsWith("M"))
                {
                  ManualTime editRecord =  listHoursLog.SelectedItem as ManualTime;
                    //Launch editor
                   ManualHours newForm = new ManualHours(editRecord);
                    newForm.ShowDialog();
                    if (newForm.ShowDialog() == DialogResult.OK)
                    {
                        if (editRecord != null)
                        {
                            listHoursLog.Items.Add(editRecord);
                        }
                    }
                }
                else
                    MessageBox.Show("You may edit only the manually entered records.");
            }
        }
    }
}

...The ManualHours editor form uses this constructor...

namespace LOG
{
    public partial class ManualHours : Form
    {
         public ManualHours()
        {
            InitializeComponent();
        }

        public ManualHours(ManualTime recordE)
        {
            InitializeComponent();
            //construct the prior data
            startDateTimePicker.Value = recordE.date;
            /*Need to parse the hours and mins to decimal*/
            hrsUpDown.Value = Convert.ToDecimal(recordE.hours.TotalHours);
            minsUpDown.Value = Convert.ToDecimal(recordE.mins.TotalMinutes);
        }

This is the ManualTime class... it is simple.

namespace LOG
{
    public class ManualTime
    {
        public DateTime date { get; set; }
        public TimeSpan hours { get; set; }
        public TimeSpan mins { get; set; }
        public TimeSpan totalTime { get; set; }
    public ManualTime()
    {
        date = DateTime.Now;
    }
    public override string ToString()
    {
        //For list of log records
        return string.Format("Manual entry; Date: {0}, ({1} hours  + {2} mins) = Total Time: {3}",
            this.date.ToShortDateString(),
            this.hours, this.mins,
            totalTime.ToString(@"hh\:mm\:ss"));
    }
}

}

Was it helpful?

Solution

I think this line is wrong:

ManualTime editRecord =  listHoursLog.SelectedItem as ManualTime;

You need to convert the selected item into a ManualTime, not cast it. The cast will always return null, hence the error.

If the selected item is a string, then you could write a Parse() function in your ManualTime class to convert a string, something like the reverse of your ToString() functions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top