无法通过此错误:对象引用未设置为对象的实例

时间:2012-04-20 21:32:14

标签: c# asp.net

这是我的代码:

    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。我不知道如何克服这个错误。提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

在第16行中,如果Session [“DistID”]为空,则情况为(int)失败。

因此,您需要将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
            }
        }
    }