2012-04-20 46 views
0

這裏的一個實例是我的代碼:無法越過這個錯誤:未將對象引用設置到對象

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session[ "DistID" ] != "") 
     { 
      DistInfo distInfo = GeneralFunctions.GetGeneralInformation((int)Session[ "DistID" ]); 
      launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); 
     } 
    } 

    public static DistInfo GetGeneralInformation (int ClientID) 
    { 
     using (var conn = new SqlConnection(GetConnectionString())) 
     using (var cmd = conn.CreateCommand()) 
     { 
      conn.Open(); 
      cmd.CommandText = 
      @"SELECT i.GoLiveDate, i.FirstBonusRun, i.TechFName, i.TechLName, i.TechEmail, i.TechPhone, i.WebISPFName, i.WebISPLName, 
       i.WebISPEmail, i.WebISPPhone, i.FullFillFName, i.FullFillLName, i.FullFillEmail, i.FullFillPhone, d.FName, 
       d.LName, d.HomePhone, d.Email 
       FROM NC_Information i 
       INNER JOIN Distributor d 
       ON d.DistID = i.ClientID 
       WHERE clientID = @value"; 
      cmd.Parameters.AddWithValue("@value", ClientID); 
      using (var reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        var distInfo = new DistInfo 
        { 
         AnticipatedLaunchDate = reader.GetDateTime(reader.GetOrdinal("GoLiveDate")) 
        }; 

        return distInfo; 
       } 
      } 
      return null; 
     } 
    } 

public class DistInfo 
{ 
    public DateTime AnticipatedLaunchDate { get; set; } 
} 

以下是錯誤:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 

Line 14:   if (Session[ "DistID" ] != "") 
Line 15:   { 
Line 16:    DistInfo distInfo = GeneralFunctions.GetGeneralInformation((int)Session[ "DistID" ]); 
Line 17:    launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); 
Line 18:   } 


Source File: c:\inetpub\wwwroot\Base\ClientAccessPortal\GeneralInformation.aspx.cs Line: 16 

數據庫可以爲返回值i.GoLiveDate(這將是AnticipatedLaunchDate)DateTime,數據庫中的空DateTime或NULL。我不知道如何克服這個錯誤。預先感謝您的幫助。

回答

1

在第16行,的情況下(INT)失敗,如果會話[「DistID 「 ] 一片空白。

因此,您需要將Session [「DistID」]分配給臨時變量,並在將其轉換爲整數之前對其進行非空值測試。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session[ "DistID" ] != "") 
     { 
      var distId = Session[ "DistID" ]; 
      if (distId != null) { 
       DistInfo distInfo = GeneralFunctions.GetGeneralInformation((int)Session[ "DistID" ]); 
       launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); 
      } else { 
       // default display value 
      } 
     } 
    } 
+0

我這樣做了,這個工作也可以嗎?會話DistInfo將只會爲空或用int填充。 if(Session [「DistID」]!= null) DistInfo distInfo = GeneralFunctions.GetGeneralInformation((int)Session [「DistID」]); launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); }' – 2012-04-20 21:42:02

+0

是的,這也應該工作。 – 2012-04-20 21:44:07

+0

好吧,謝謝,到下一個錯誤。這是我的痛苦!感謝每一位的幫助。 – 2012-04-20 21:45:08

2

Session["DistID"]返回null,您嘗試將其轉換爲整數。

您檢查一個空字符串,但空值將通過檢查 - 使用IsNullOrEmpty或如果.Net 4.0 IsNullOrWhitespace來檢查兩者。

string distId = Session[ "DistID" ]; 
if (!string.IsNullOrWhiteSpace(distId)) 
{ ... } 

現在你知道你有一個非空非空字符串,可以肯定它的一個int:

string distId = Session[ "DistID" ];  
int distIdValue; 

if(!string.IsNullOrWhiteSpace(distId) && integer.TryParse(distId, out distIdValue)) 
{ 
    DistInfo distInfo = GeneralFunctions.GetGeneralInformation(distIdValue); 
    launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); 
} 
相關問題