我犯了一个我想不通的错误.对象引用未设置为对象的实例.我哪里出错了

本文关键字:设置 对象 实例 错了 出错 错误 想不通 一个 对象引用 | 更新日期: 2023-09-27 18:28:27

这是我在第144行的代码

peopleEntering.Add(new PersonEntered(personName, dateEntered, doorTypeEnum));

是我得到的错误:对象引用没有设置为对象的实例。为什么?

  protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName == string.Empty)
        {
            return;
        }
        else
        {
            string[] FileExt = FileUpload1.FileName.Split('.');
            string FileEx = FileExt[FileExt.Length - 1];
            if (FileEx.ToLower() == "csv")
            {
                FileUpload1.SaveAs(Server.MapPath(" " + FileUpload1.FileName));
            }
            else
            {
                return;
            }
        }
        CSVReader reader = new CSVReader(FileUpload1.PostedFile.InputStream);
        string[] headers = reader.GetCSVLine();
        DataTable dt = new DataTable();
        foreach (string strHeader in headers)
            dt.Columns.Add(strHeader);
        string[] data;
        while ((data = reader.GetCSVLine()) != null)
            dt.Rows.Add(data);
        foreach (DataRow row in dt.Rows)
        {
            string dateAndTime = row["Date and Time"].ToString();
            string personName = row["Description #2"].ToString();
            string doorType = row["Card number"].ToString();
            DateTime dateEntered = Convert.ToDateTime(dateAndTime);
            DoorType doorTypeEnum;
            bool personExists = false;
            foreach (object name in listboxOfNames.Items)
            {
                if (name.ToString() == personName)
                {
                    personExists = true;
                    break;
                }
            }
            if (!personExists)
            {
                listboxOfNames.Items.Add(personName);
            }
            switch (doorType)
            {
                case "A02 - Rear Entrance":
                    doorTypeEnum = DoorType.RearEntranceDoor;
                    break;
                case "B12 - Exterior Main Floor Man Trap":
                    doorTypeEnum = DoorType.ExteriorMainFloorDoor;
                    break;
                case "B12 - Interior Main Floor Man Trap":
                    doorTypeEnum = DoorType.InteriorMainFloorDoor;
                    break;
                case "C13 - Rear Break Room Door":
                    doorTypeEnum = DoorType.RearBreakRoomDoor;
                    break;
                case "B02 - Exterior Basement Man Trap":
                    doorTypeEnum = DoorType.ExteriorBasementDoor;
                    break;
                case "B02 - Interior Basement Man Trap":
                    doorTypeEnum = DoorType.InteriorBasementDoor;
                    break;
                case "D01 - Managed Services Main door":
                    doorTypeEnum = DoorType.ManagedServicesDoor;
                    break;
                case "D01 - Managed Services Big Door":
                    doorTypeEnum = DoorType.ManagedServicesBigDoor;
                    break;
                default:
                    doorTypeEnum = DoorType.None;
                    break;
            }
            peopleEntering.Add(new PersonEntered(personName, dateEntered, doorTypeEnum));
        }

        for (int i = 0; i < peopleEntering.Count; i++)
        {
            DateTime startDate = new DateTime();
            DateTime endDate = new DateTime();
            string personName = peopleEntering[i].PersonName;
            if ( peopleEntering[i].DoorEntered == DoorType.RearEntranceDoor)
            {
                startDate = peopleEntering[i].DateOfEntry;
                for ( int j = i + 1; j < peopleEntering.Count; j++)
                {
                    if (peopleEntering[j].DoorEntered == DoorType.ExteriorBasementDoor && peopleEntering[j].PersonName == personName)
                    {
                        endDate = peopleEntering[j].DateOfEntry;
                    }
                }
            }
            workSpans.Add(new WorkSpan(personName, startDate, endDate));
        }
        csvReaderGv.DataSource = dt.Rows;
        csvReaderGv.DataBind();
    }


            }   

我犯了一个我想不通的错误.对象引用未设置为对象的实例.我哪里出错了

在该特定行上,NullReferenceException的唯一可能来源是如果peopleEnteringnull。该语句中的所有其他内容都是一个新表达式或本地/字段读取。从发布的方法来看,这似乎是方法中的一个字段与一个局部。您是否记得将其初始化为非零值(假设类型为List<PersonEntered>

private List<PersonEntered> peopleEntering = new List<PersonEntered>();

这只是一个猜测,因为您正在使用ASP.NET(如前一个问题所示):

我假设您已经初始化了List一次,可能使用IsPostBack检查。

private List<PersonEntered> peopleEntering;
protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
         peopleEntering = GetPeropleEntering(); // a method that retrieves them from database
    }
}

在这种情况下,您必须在每次回发时重新加载列表(删除IsPostBack检查),或者必须将其存储在某个位置,以便在回发时保持其状态。CCD_ 8是一种方式。请记住,所有变量(包括所有控件)都是在页面生命周期结束时处理的。

以下是其他8个:http://msdn.microsoft.com/en-us/magazine/cc300437.aspx