James Wilson
James Wilson

Reputation: 5150

Cannot get past this error: Object reference not set to an instance of an object

Here is my code:

    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; }
}

Here is the error:

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 

The database can return the value for i.GoLiveDate (which will be AnticipatedLaunchDate) of a DateTime, an empty DateTime or NULL from the database. I'm not sure how to get past this error. Thanks in advance for any help.

Upvotes: 0

Views: 591

Answers (2)

asawyer
asawyer

Reputation: 17808

Session["DistID"] is returning null, which you try to cast into an integer.

Your checking for an empty string, but null values will past that check - use IsNullOrEmpty or if .Net 4.0 IsNullOrWhitespace to check both.

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

Now that you know you have a non null non empty string, be sure its an 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();
}

Upvotes: 2

Philipp Schmid
Philipp Schmid

Reputation: 5828

In line 16, the case to (int) is failing if Session[ "DistID" ] is null.

So you will need to assign Session[ "DistID" ] to a temporary variable and test it for non-null before casting it to an integer.

    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
            }
        }
    }

Upvotes: 1

Related Questions