-2

im trying to put a double - price in a label which will show the full price.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType==DataControlRowType.Footer)
    {
        Label labelFooter = (Label)e.Row.Cells[5].FindControl("LabelFooter");
        Cart c = (Cart)Session["cart"];
        double sum = c.TotalPrice();
        labelFooter.Text = sum.ToString();
    }
}

and then I get for the last line(labelFooter.Text = sum.ToString()):

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

edit:ok the problem is not with the convert

SomeGuy
  • 1
  • 2

1 Answers1

0

A NullReferenceException can be caused by multiple reasons. For example, if e is null. Perhaps the line that declares Cart c throws a null. Also, to me, Cart looks like a locally-defined class. It may help a lot to see its content.

Nevertheless, one cause of null rejection is declarating but not implementing a field before its first amendment is made. Like for example the field "vocab" here:

public class ExampleClass{
        private Dictionary<string, string> vocab;
        
        public ExampleClass(){
        vocab.Add("rain", "Regen");// this will raise a nullref exception
    }
}

Fix for my code snippet above is to add vocab = new Dictionary<string, string>(); in the constructor before the first call of the Add method.

a0poster
  • 65
  • 6