2013-08-20 29 views
0

這裏是我的代碼,在網上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(); 
    } 




      } 
+2

請避免發佈您的整個源代碼;只發布相關行。空引用異常都是一樣的...只要進入一個調試器,並檢查爲什麼你的對象爲空。 'peopleEntering'可能未被初始化,或者在該行之前顯式設置爲null,沒有時間讀取_all_該代碼。 –

+0

您是否確定實例化peopleEntering?看起來情況可能如此。 – Mani5556

+2

您尚未顯示'peopleEntering'的聲明或初始化。我懷疑你永遠不會給它一個非空值。 –

回答

4

在該特定線NullReferenceException的唯一可能來源是如果peopleEnteringnull。該聲明中的所有內容都是新的表達式或本地/字段讀取。看看發佈的方法,它顯示這是一個字段與本地方法。你記得它初始化爲一個非空值(假設類型爲List<PersonEntered>

+0

錯誤也可能來自'PersonEntered'構造函數;如果它標有'[DebuggerNonUserCode]'或[DebuggerStepThrough]'(不知道當前是哪一個),那麼調試器不會停止在構造函數中,而是在呼叫站點,即該線路上。 – stakx

+0

@stakx這是非常真實的。我以他們的話來說,這是實際發生異常的地方。 – JaredPar

+1

@Jay,[只是將最能幫助你的答案標記爲Accepted](http://stackoverflow.com/help/someone-answers),如果你解決了你自己的問題[你可以發佈自己的問題答案和標記,而不是](http://stackoverflow.com/help/self-answer)。 –

1

只是一個猜測,因爲你正在使用ASP.NET(在你prev. question所示):

我認爲您已初始化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檢查)列表,或者你必須存儲在某個地方保留它在回髮狀態。 Session是一種方法。請記住,所有變量(包括所有控件)都放置在頁面生命週期的末尾。

這裏有8人:http://msdn.microsoft.com/en-us/magazine/cc300437.aspx

相關問題