Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实例化的空变量、空引用错误和新形式的构造函数_C#_Winforms_Casting_Listboxitem - Fatal编程技术网

C# 实例化的空变量、空引用错误和新形式的构造函数

C# 实例化的空变量、空引用错误和新形式的构造函数,c#,winforms,casting,listboxitem,C#,Winforms,Casting,Listboxitem,我当前在一个代码块上遇到此错误。请就如何解决这个问题提出意见或建议。多谢各位 "An unhandled exception of type 'System.NullReferenceException' occurred in HHONamespace.exe" Additional information: Object reference not set to an instance of an object." 问题很简单,在传递editRecord变量cast as Man

我当前在一个代码块上遇到此错误。请就如何解决这个问题提出意见或建议。多谢各位

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

Additional information: Object reference not set to an instance of an object."
问题很简单,在传递editRecord变量cast as ManualTime之前,当它应该具有列表框中的所选记录值时,我得到一个null。我测试了调试器,它读取null,即使它是一个列表记录,它是由同一个ManualTime类的重写构造而成的,以便从listBox将其强制转换为。这很难解释,到底发生了什么?为什么断开连接以将属性写入此变量后面的编辑器窗体?编辑器表单中的构造函数失败,因为此变量实例是空引用。代码一直工作到数据结构的最终转换,这很奇怪

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.");
            }
        }
    }
}
…ManualHours编辑器窗体使用此构造函数

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);
        }
这是手动时间类。。。这很简单

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"));
    }
}

}我认为这句话是错误的:

ManualTime editRecord =  listHoursLog.SelectedItem as ManualTime;
您需要将所选项目转换为手动时间,而不是强制转换它。强制转换将始终返回null,因此会出现错误


如果所选项目是字符串,则可以在ManualTime类中编写Parse()函数来转换字符串,类似于ToString()函数的相反形式。

能否向我展示用于绑定
listHoursLog
的代码。我认为
listHoursLog.SelectedItem
不能直接强制转换为
ManualTime
,因此它返回
null
insteadIt将显示属性应该能够使用该项。我有一个讲师给我的小部件的另一个例子,小部件行只返回它们。我没有看到特殊的解析方法。但你可能是对的我可能需要这么做。与您所说的问题类似,我有一个自定义方法将列表项添加到其他地方。它还强制转换为列表中每个项的字符串,这是一个问题,需要将其转换为字符串()。在我将记录添加到列表框时,写入记录的对象表示将其设置为字符串。它只需要是对象类型,而不是tostring,这样它就无法分辨差异,tostring覆盖需要是隐式的,这样它就保持为ManualTime对象。我修复了这两种方法,现在可以使用了。铸造并不总是正确的!谢谢