2012-04-20 4 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 

データベースの値を返すことができますDateTimeのi.GoLiveDate(AnticipatedLaunchDate)、空のDateTimeまたはNULL(データベースから)。このエラーをどのように回避するかわかりません。助けを前にありがとう。

答えて

1

、(int型)の場合は、DistID」[セッション場合は失敗しています" ] 無効である。

したがって、Session ["DistID"]を一時変数に割り当てて、整数にキャストする前に非nullをテストする必要があります。

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は、これまでのところnullになるか、intで埋められます。 '(Session [" DistID "]!= null) { DistInfo distInfo = GeneralFunctions.GetGeneralInformation(()Session [" DistID "]); launchDate.Text = distInfo.AnticipatedLaunchDate.ToShortDateString(); } ' –

+0

はい、うまくいくはずです。 –

+0

ありがとう、次のエラーに。これは私の底の痛みです!すべての人に助けてくれてありがとう。 –

2

Session["DistID"]がnullを返しています。これを整数にキャストしようとしています。

空の文字列をチェックしてもnull値はそのチェックを過ぎます。IsNullOrEmptyまたは.Net 4.0 IsNullOrWhitespaceを使用して両方をチェックしてください。

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

今、あなたはあなたがnull以外の非空の文字列を持って知っていること、そのAN int型を確認してください:ライン16で

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(); 
} 
関連する問題